范围查询
可以是连续的范围或者不连续的范围
- in 表示在一个非连续的范围内
- between … and … 表示在一个连续的范围内
例子
- 查询编号是1或3或8的学生
select * from students where id in(1,3,8); - 查询编号是3到8的男生
select * from students where id between 3 and 8 and gender=1;
注意:and如何匹配?
and既出现在逻辑运算符内,又出现在betweene and 里。那么如何匹配呢?
and作为逻辑运算符的优先级是很低的,and会优先和between匹配。
空判断
- 注意:null与’'是不同的;null(或者python里的None)指的是不占任何内存,’‘是空字符串。
- 判断为空 is null
- 判断为非空 is not null
例子
- 生日为空的学生
select * from students where birthday is null; - 查询填写了地址的女生
select * from students where address is not null and gender=0;
优先级
- 小括号,not,比较运算符,逻辑运算符
- and比or先运算,如果同时出现并希望先算or,需要结合()使用