另起昵称
select*from UserInfo as ui
as 可省略起别名
*代表查询所有列
select ui.Username as 姓名,ui.UserPwd as 密码
查询部分列并重命名
Tips:从某个表中查找时,如果已经改名就必须用改过的名字
筛选
- 绝对值筛选`
select top 1*
from UserInfo
- 百分比筛选
select top 2 percent*
from UserInfo
排序筛选
排序其中desc表示由大到小,aesc表示由大到小
select* from 资料表
order by 编号 desc,sid desc
消除某些行
Select distinct 编号 from 资料表
可以看到结果如下:
条件查询
查具体某个属性
select 姓名 from 资料表
where 编号=1
查编号为连续区间内的:
select * from 资料表
where 编号>=3 and 编号<=6
或者
select * from 资料表
where 编号 between 3 and 6
逻辑运算符:and,or,not
where CID=1 or cid=3
OR,可以是不同列的,但是上面的必须是同一列的。
同理and语法
--取学生编号为3-8的在1班的学生
select*from Test
where USER_SID between 3 and 8 and USER_ID=1
select*from Studentinfo
where not ((SID between 3 and 8)and (cid=1))
我们可以看出结果是不是3-8的和cid不是1的,可以从分配律理解1*(1+1)=11+11,其中加是与的意思,或者并不是并列