mysql数据库where判断条件子句语法代码
where后面跟的是条件,在数据源中进行筛选。返回条件为真记录 MySQL支持的运算符 - -- 比较运算符
- > 大于
- < 小于
- >= 大于等于
- <= 小于等于
- = 等于
- != 不等于
- -- 逻辑运算符
- and 与
- or 或
- not 非
- -- 其他
- in | not in 字段的值在枚举范围内
- between…and|not between…and 字段的值在数字范围内
- is null | is not null 字段的值不为空
复制代码例题:
- -- 查找语文成绩及格的学生
- mysql> select * from stu where ch>=60;
- -- 查询语文和数学都及格的学生
- mysql> select * from stu where ch>=60 and math>=60;
- -- 查询语文或数学不及格的学生
- mysql> select * from stu where ch<60 or math<60;
复制代码思考:如下语句输出什么?
- mysql> select * from stu where 1; -- 输出所有数据
- mysql> select * from stu where 0; -- 不输出数据
复制代码思考:如何查找北京和上海的学生
- -- 通过or实现
- mysql> select * from stu where stuaddress='北京' or stuaddress='上海';
- -- 通过in语句实现
- mysql> select * from stu where stuaddress in ('北京','上海');
- -- 查询不是北京和上海的学生
- mysql> select * from stu where stuaddress not in ('北京','上海');
复制代码思考:查找年龄在20~25之间
- -- 方法一:
- mysql> select * from stu where stuage>=20 and stuage<=25;
- -- 方法二:
- mysql> select * from stu where not(stuage<20 or stuage>25);
- -- 方法三:between...and...
- mysql> select * from stu where stuage between 20 and 25;
- -- 年龄不在20~25之间
- mysql> select * from stu where stuage not between 20 and 25;
复制代码思考:
- -- 查找缺考的学生
- mysql> select * from stu where ch is null or math is null;
- +--------+----------+--------+--------+---------+------------+------+------+
- | stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |
- +--------+----------+--------+--------+---------+------------+------+------+
- | s25301 | 张秋丽 | 男 | 18 | 1 | 北京 | 80 | NULL |
- | s25304 | 欧阳俊雄 | 男 | 28 | 4 | 天津 | NULL | 74 |
- +--------+----------+--------+--------+---------+------------+------+------+
- -- 查找没有缺考的学生
- mysql> select * from stu where ch is not null and math is not null;
- +--------+----------+--------+--------+---------+------------+------+------+
- | stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |
- +--------+----------+--------+--------+---------+------------+------+------+
- | s25302 | 李文才 | 男 | 31 | 3 | 上海 | 77 | 76 |
- | s25303 | 李斯文 | 女 | 22 | 2 | 北京 | 55 | 82 |
- | s25305 | 诸葛丽丽 | 女 | 23 | 7 | 河南 | 72 | 56 |
- | s25318 | 争青小子 | 男 | 26 | 6 | 天津 | 86 | 92 |
- | s25319 | 梅超风 | 女 | 23 | 5 | 河北 | 74 | 67 |
- | s25320 | Tom | 男 | 24 | 8 | 北京 | 65 | 67 |
- | s25321 | Tabm | 女 | 23 | 9 | 河北 | 88 | 77 |
- +--------+----------+--------+--------+---------+------------+------+------+
- 7 rows in set (0.00 sec)
- -- 查找需要补考的学生
- mysql> select * from stu where ch<60 or math<60 or ch is null or math is null;
- +--------+----------+--------+--------+---------+------------+------+------+
- | stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |
- +--------+----------+--------+--------+---------+------------+------+------+
- | s25301 | 张秋丽 | 男 | 18 | 1 | 北京 | 80 | NULL |
- | s25303 | 李斯文 | 女 | 22 | 2 | 北京 | 55 | 82 |
- | s25304 | 欧阳俊雄 | 男 | 28 | 4 | 天津 | NULL | 74 |
- | s25305 | 诸葛丽丽 | 女 | 23 | 7 | 河南 | 72 | 56 |
- +--------+----------+--------+--------+---------+------------+------+------+
- 4 rows in set (0.00 sec)
复制代码
|