條件查詢
條件查詢就是在查詢時給出where子句,在where子句中可以使用一些運算符及關鍵字。
=(等于)、!=(不等于)、<>(不等于)、<(小于)、<=(小于等于)、>(大于)、>=(大于等于)
between....and;值在什么范圍
in(set);固定的范圍值
is null;(為空)
is not null; (不為空)
and 與
or 或
not 非
使用
- 查詢性別為男,且年齡為20的學生記錄
mysql> select * from students where gender='男' and age=20;
- 查詢學號為2,或者名為denve的學生記錄
mysql> select * from students where id=2 or name='denve';
- 查詢學號為1,2,5,的學生記錄
#方法一
mysql> select * from students where id=1 or id=2 or id=5;
#方法二
mysql> select * from students where id in(1,2,5);
4.查詢年齡為Null的學生記錄
mysql> select * from students where name is null;
5.查詢年齡不為Null的學生記錄
mysql> select * from students where name is not null;
6.查詢性別非男的學生記錄
mysql> select * from students where gender!='男';
7.查詢年齡在18到20歲之間的學生記錄
#方法一
mysql> select * from students where age>=18 and age<=20;
#方法二
mysql> select * from students where age between 18 and 20;