zoukankan      html  css  js  c++  java
  • 数据查询基础

      

    复习:
    --查询:select * from 表名
    --添加:insert [into] 表名(列名)values(值)

    Default关键字
    第一种:通过insert select 语句添加(将现有表中的数据添加到已存在的表中)
    Insert into <表名>(列名)
    Select <列值>
    From<源表名>
    第二种:通过select into语句添加(将现有表中的数据添加到新表:不创建新表)
    Select(列名)
    Into <新表名>
    From <源表名>
    第三种:通过Union关键字来多行插入数据
    Insert into<表名>(列名)
    Select <列的值>union
    ……………
    Select <列的值>
    --修改:update 表名 set 列名 = 更新值 where 更新条件
    --删除:delete from 表名 where 条件

     

    使用select语句进行查询
    语法:select <列名/* >
    From <表名>
    Where <查询条件表达式>
    [Order by<排序的列名>[asc或desc]]

    查全部的行和列
    -- 查询所有学生信息
    select * from Students--ScoreCourse

    --查询学生的姓名和地址
    select SName,SAddress from Students

    --查询“北京”的学生
    select SName,SAddress from Students where SAddress = '北京海淀'

    --select * from Students where SAddress like '%北京%'

    select SName,SAddress,SEmail from Students where SSex = 1

    查部分列
    -- 查部分列
    select SCode,SName,SAddress from Students where SAddress = '四川成都'
    select SCode,SName,SAddress from Students where SAddress <> '四川成都'


    列别名
    AS关键字
    --列的别名
    select SName as 学生姓名,SAddress AS 家庭住址 from Students
    --列的别名
    select SName as 学生姓名,SAddress AS 家庭住址 from Students where SAddress <> '上海松江'

    使用“=”命名列
    select SName +'~'+ SName AS 姓名 from Students

    select 姓名 = SName +'~'+ SName from Students

    查询空行
    select * from Students where SEmail is null
    select * from Students where SEmail = ''

    --查询常量值
    select *,'清华大学' as School from Students where SGrade = 1
    select *,'北京大学' as School from Students where SGrade = 2


    --限制行数(限制固定行数)
    select top 2 * from Students [where SName = '']
    --按百分比返回行
    select top 50 percent * from Students [where SName = '']

    查询单列排序
    --Order by 升序
    select StudentID,Score from Score Order by Score
    select StudentID as 学生编号,(Score * 0.9 + 5) as 综合成绩
    from Score
    where (Score* 0.9 + 5) > 60
    Order by Score

    --降序排序
    select StudentID,Score from Score Order by Score DESC
    --查询多列排序
    select StudentID as 学生编号,CourseID as 课程编号,Score as 成绩
    from Score
    where Score > 60
    Order by CourseID,Score

    小结
    select * from Student where Address LIKE '%河北%' and Sex = '男'

    --连表查询
    -- Result连接Student表,通过Result表中的StudentNo连接Student表中的StudentNo
    查询成绩表中的前5条数据,并显示姓名和成绩这两列,成绩按照降序排列
    select top 5 StudentName,StudentResult from Result inner join Student a
    on Result.StudentNo = a.StudentNo
    Order by StudentResult DESC


    Sql Server中的函数
    --寻找一个指定的字符串在另一个字符串中的起始位置
    select CHARINDEX('name','My is Tom age name')
    --返回传递给它的字符串长度
    select LEN('Java 课程')
    --把传递给它的字符串转换为大写
    select UPPER('java ')--JAVA
    --清除字符左边的空格
    select LTRIM(' 智障 ')
    --清除字符右边的空格
    select RTRIM('智障 = 智障 ')
    --从字符串右边返回指定数目的字符
    select right('买买提, 吐尔松',7)
    --替换一个字符串中的字符
    select REPLACE('莫乐可切.杨可','可','兰')
    --在一个字符串中,删除指定长度的字符,并在该位置插入一个新的字符串
    select STUFF('ABCDE',2,3, '我的音乐我的世界')--从开始数个

    --日期函数
    select GETDATE() as Time
    --将指定的数值添加到指定的日期部分后的日期
    select DATEADD(WW,5,'01/01/2009')--第一个参数是年月日的缩写,第二个要累加的数字,第三个日期格式
    --两个日期之间的指定日期部分的间隔
    select DATEDIFF(DD,'2001/01/01','2002/01/01')
    --日期中指定日期部分的整数形式
    select DATEPART(DAY,'2000/01/9')
    --日期中指定日期部分的字符串形式
    select DATENAME(DW,'2000/01/03')


    --数学函数
    --返回从0 到1 之间的随机float 值
    select RAND()
    --取数值表达式的绝对值
    select ABS(-50)
    --取大于或等于指定数值、表达式的最小整数
    select CEILING(43.00000000000000000001)
    --取小于或等于指定表达式的最大整数
    select FLOOR(43.0000000000000000000009)
    --取数值表达式的幂值
    select POWER(2,10)--2,100一处错误.000000。
    --将数值表达式四舍五入为指定精度
    select ROUND(43.05,1)
    --对于正数返回+1,对于负数返回-1,对于则返回
    select SIGN(0)
    --取浮点表达式的平方根
    select SQRT(3847289)


    --系统函数
    select CONVERT(varchar(6),12345)
    --返回当前用户的名字
    select CURRENT_USER
    --返回用于指定表达式的字节数
    select DATALENGTH('abcd 汉字')--汉字就是两个
    --返回当前用户所登录的计算机名字
    select HOST_NAME()
    --返回当前所登录的用户名称
    select SYSTEM_USER
    --从给定的用户ID返回用户名
    select USER_NAME(1)

     

     

     

     

     

  • 相关阅读:
    Timer 实现2秒4秒连环炸
    Java中的注解
    PHP连接打印机
    php同步mysql两个数据库中表的数据
    thinkphp 两表、三表联合查询
    ereg/eregi报错处理办法
    ThinkPHP3.2判断手机端访问并设置默认访问模块的方法
    使用PHP获取时间今天 明天 昨天 时间戳的详解
    jquery获取radio和select选中值
    php开启mysqli扩展之后如何连接数据库
  • 原文地址:https://www.cnblogs.com/SFHa/p/8259134.html
Copyright © 2011-2022 走看看