where子句可以有条件地从表中选取数据,可将 where子句添加到 select语句。
where子句可以使用的运算符:
注意:在某些版本的 SQL 中,操作符 <> 可以写为 !=。
where子句可以使用的条件连接符:
1) and:a and b,选取要a和b都为真的数据子集
2) or:a or b,选取要a或b都为真的数据子集
3) ():当条件为3个或以上时,常常需要用到,比如 a and (b or c)
4) not:not a,选区非a的数据子集,增加了对条件评估的难度,一般尽量避免使用
语法:
select column_name ,column_name1
from table_name
where table_name operator value
示例:
表的定义如下
create table [dbo].[Students](
[StuId] [int] not null,
[StuName] [nvarchar](50) not null,
[StuDept] [nvarchar](50) not null,
[StuAge] [int] not null
)
1.普通查询:
select * from[dbo].[Students]
2.查询水务局的学生的所有信息
select [StuId]
,[StuName]
,[StuDept]
,[StuAge]
from [Students]
where StuDept='水务局'
3.查询年纪大于25岁学生的所有信息
select [StuId]
,[StuName]
,[StuDept]
,[StuAge]
from [Students]
where StuAge>25
4.查询年龄大于30岁且李姓的学生所有信息
select [StuId]
,[StuName]
,[StuDept]
,[StuAge]
from [Students]
where StuAge>30 and StuName like '李%'
end有兴趣的小伙伴可以关注“SQL数据库笔记”公众号,一起学习吧!