查询
1、为表起别名as
select * from classinfo as ui
查询全部列、指定列
为列起别名as
select ui.cid from classinfo ui
2、查询前n部分数据:
top n 列名:表示查看前n行
select top 3 * from classinfo--查询前三行所有列
top n percent 列名:表示查看前百分之几的数据
select top 5 percent *from classinfo--查询前百分之五的所有列数据
3、排序:order by 列名1 asc(升序)|desc(降序),列名1 asc|desc…
select * from Userinfo
order by cid desc ,cname desc--先根据cis降序排,再根据cname升序排
4、消除重复行:distinct
select distinct cname from classinfo--消除classinfo里的cname相同的(只是查询时消除,并不是删除数据库里的)
5、条件查询:写在where后面
对行进行筛选,返回bool类型的值,如果某行中的列数据满足条件,则加入结果集,否则不出现在结果集中
比较运算符:=,>,>=,<,<=,!=或<>
select * from classinfo
where cid>3
between ... and ...表示在一个连续的范围内
select * from classinfo
where cid between 2 and 4
in表示在一个非连续的范围内
select * from classinfo
where cid in(3,5)
逻辑运算符:and,or,not
select * from classinfo
where cid=3 or cid=5
select * from classinfo
where not cid>=2 and cid<=5
6、模糊查询:
用于处理字符串类型的值,运算符包括:like % _ [] ^
%若干个
select * from studentinfo
where sphone like '%4%'--电话号中含4
_单个
select * from studentinfo
where sname like '虎_'--虎某某
[]范围
在[]表示一个连续的范围可以使用-
select * from studentinfo
where sphone like '1[3-5]%'--电话号1开头第二个数字时3到5范围内的电话号
%与_写在[]中表示本身的含义
^写在[]内部的开头,表示不使用内部的任何字符
select * from studentinfo
where sphone like '1[^553]%'--电话号1开头接着非553内的电话号
7、null的判断:使用is null或is not null,与其它值计算时返回null,排序时null被认为是最小
select * from studentinfo
where sphone is null--查询电话是空的