where子句对行进行筛选
后面可跟 算术表达式(+ - * /)、关系表达式(<>=)、逻辑表达式(not、and、or)
- 关系运算符
select*from sc where 成绩>60
select 姓名,性别,班级 from student where left(姓名,l)=‘赵’
select 姓名,性别,班级 from student where year(出生日期)>1990
select Sname from student where sdept!=‘cs’
select distinct sno from sc where grade<60
- 逻辑运算符
select * from sc where not 成绩>=60
#使用逻辑运算符not
#not:返回不满足表达式的行
select * from student where 班级=‘4’and 性别=‘男’
#使用逻辑运算符and
select * from course where 考试类型=‘机考’or 学分=3
#使用逻辑运算符or
- 范围运算符
select sname,sdept,sage from student where sage between 20 and 23
select sname,sdept,sage from student where sage not between 20 and 23
- 列表运算符
select sname,ssex from student where sdept in('is','ma','cs')
# 使用列表运算符in
select sname,ssex from student where sdept not in('is','ma','cs')
#使用列表运算符not in
- 模糊匹配运算符
select 字段 from 表 where 某字段 like 条件
表达 | 含义 | 示例 |
---|---|---|
[not] like +% | %表示任意0个或多个字符,可匹配任意类型和长度的字符 | select sname,sno,ssex from student where sname like ‘刘%’ |
[not] like +“_” | _:表示任意单个字符。它常用来限制表达式的字符长度 | select sname from student where sname like ‘_小刚’ |
[not] like +[ ] | [ ]:指定范围或集合内的任何单个字符。例如:[a-f],[abcdef],要求所匹配对象为他们中任一个 | select * from persons where city like '[aln]%'查询居住的城市以“A”或“L”或“N”开头的 |
[not] like + [^] | [^]:表示不在括号所列之内的单个字符。其取值和[ ]相同,但它要求所匹配对象为指定字符以外的任一个字符。 | select * from user where name like ‘[^张李王]三’ 将找出不姓“张”、“李”、“王”的“赵三”、“孙三”等 |
- 空值运算符
select sno,cno from sc where grade is null
条件表达式可使用多重条件查询
逻辑运算符:and 和 or 来联结多个查询条件
- and的优先级高于or
- 但可以用括号改变优先级
select sname from student where sdept=‘cs’and sage<20
select sname,SSex from student where sdept='is' or sdept='ma' or sdept='cs'