查询数据
简单的查询
1 create table stu_info 2 ( 3 sno int not null 4 ,sname varchar(20) not null 5 ,sex varchar(2) not null 6 ,birth varchar(20) not null 7 ,email varchar(20) not null 8 ,telephone int not null 9 ,depart varchar(20) not null 10 ) 11 12 13 select distinct depart from dbo.stu_info 14 -- select order by depart from dbo.stu_info 15 --select sname ,datediff(year,birth,getdate()) from dbo.stu_info 16 --命名 17 select sname as 姓名 from dbo.stu_info 18 -- 把查询结果保存为一个新表 19 20 select sname as 姓名 into sname2 from dbo.stu_info 21 -- 查询 sname2 即使用了 into后 22 select * from sname2 23 --链接表字段 24 select sname+depart as 姓名来源 from dbo.stu_info
指定条件的查询 关键字 where
用到两个概念 指针和字段
条件表达式
条件运算符(这里列举我自己还没掌握的):SQL特殊条件运算符:
in :在某个集合中 学分(2,3,4)
not in : 。。。
between : 在某个范围 学分 between 2 and 3
not between : 。。。。。。
like : 与某种模式匹配 姓名 like '%三%' //似乎是通配符(第一感觉)
not like : 。。。。。。
is NULL : 是NULL值 联系方式 2 is NULL // 这里只能大写 NULL
is nut NULL : 。。。。。。
1 -- where 2 select * from dbo.stu_info where sno>3 3 select * from dbo.stu_info where sno=3 4 select sno,sname,sex,depart from dbo.stu_info where sname>'李四' 5 -- 查询日期数据 SQL 默认格式 月/日/年 6 select sno,birth as 生日,date from dbo.stu_info where date>'01/05/1980' 7 select sno,birth as 生日,date from dbo.stu_info where date<'01/05/1980' and date>'01/05/1790' 8 -- 按范围查询数据 9 select * from dbo.stu_info where sno between 2 and 4 10 select * from dbo.stu_info where email is not null 11 12 --排序查询数据 order by 后接 按哪个字段排名 13 select sno,sname,birth from dbo.stu_info order by sname 14 15 -- 设置排名方向 asc 升 desc 降 16 select sno,sname,birth from dbo.stu_info order by sname desc 17 -- 按多列排序 在desc 后加上需要排序的字段 即可 (升序) 18 --........ 19 -- 按字段位置排序 有时表达式过长,这样减少错误率 , 下面的2 代表select后面字段的第二个值 20 select sname ,datediff(year,date,getdate()) as 年龄 from dbo.stu_info order by 2 desc 21 22 -- 查询前 几(2)行数据 关键字 top 23 select top 2 sno,sname,birth from stu_info order by birth 24 -- 查询前 n)行数据 关键字 top percent 百分之n 25 select top 2 percent sno,sname,birth from stu_info order by birth 26 27 -- where 与 order by 结合使用 where 一定在前 28 select sno,sname,telephone,depart from stu_info where telephone is not null order by sno desc 29 30
高级条件查询
1 select * from stu_info where depart in ('中文系','外语系','计算机系') 2 order by depart asc 3 4 -- like 与 % 通配符 模糊查询 %代表0个或多个字符 5 6 select * from stu_info where sname like '%三%' 7 --为了更好的体现like+% 的作用,插入几条语句 8 insert into stu_info ( sno,sname,sex,birth,email,telephone,depart,date) 9 values(6,'刘三姐','男','zz','zhiniao@gmail.com','123456765','软件系','05/15/1983') 10 select * from stu_info 11 select * from stu_info where sname like '%三%' 12 select * from stu_info where sname like '三%' 13 --rtrim 将右边的空格除去 14 select * from stu_info where rtrim(sname) like '%三' 15 -- 指定个数的字符 ' _ ' 16 select * from stu_info where sname like '刘%' 17 select * from stu_info where sname like '刘_' --按通配符_个数匹配
通配符 [] :
[nr]% 代表以"n"或"r"字母开头的所有字符串
[a-d]%img 代表以"a"、"b"、"c"、"d"字母开头,以"img"结尾的所有字符串
n[^b]% 代表以"n"字母开头,并且第二个字母不是"b"的所有字符串
其它的自己琢磨 举三反全
1 -- 通配符 [] 2 select * from stu_info where sname like '[张李]%' 3 select * from stu_info where sname like '[^张李]%'
定义转义字符 escape “ like '%5#%' escape '#' ”
1 select * from stu_info where sname like '[^张李]#%' escape '#'