您可以使用 IN 子句替换许多 OR 条件
要了解 IN 子句,请考虑一个 employee_tbl 表,该表具有以下记录-
mysql> SELECT * FROM employee_tbl; +------+------+------------+--------------------+ | id | name | work_date | daily_typing_pages | +------+------+------------+--------------------+ | 1 | John | 2007-01-24 | 250 | | 2 | Ram | 2007-05-27 | 220 | | 3 | Jack | 2007-05-06 | 170 | | 3 | Jack | 2007-04-06 | 100 | | 4 | Jill | 2007-04-06 | 220 | | 5 | Zara | 2007-06-06 | 300 | | 5 | Zara | 2007-02-06 | 350 | +------+------+------------+--------------------+ 7 rows in set (0.00 sec)
现在,假设基于上表,您要显示的daily_typing_pages分别等于250、220和170的记录。可以使用 OR 条件,如下所示
mysql>SELECT * FROM employee_tbl ->WHERE daily_typing_pages= 250 OR ->daily_typing_pages= 220 OR daily_typing_pages= 170; +------+------+------------+--------------------+ | id | name | work_date | daily_typing_pages | +------+------+------------+--------------------+ | 1 | John | 2007-01-24 | 250 | | 2 | Ram | 2007-05-27 | 220 | | 3 | Jack | 2007-05-06 | 170 | | 4 | Jill | 2007-04-06 | 220 | +------+------+------------+--------------------+ 4 rows in set (0.02 sec)
使用 IN 子句可以实现相同的效果,如下所示-
mysql> SELECT * FROM employee_tbl -> WHERE daily_typing_pages IN ( 250, 220, 170 ); +------+------+------------+--------------------+ | id | name | work_date | daily_typing_pages | +------+------+------------+--------------------+ | 1 | John | 2007-01-24 | 250 | | 2 | Ram | 2007-05-27 | 220 | | 3 | Jack | 2007-05-06 | 170 | | 4 | Jill | 2007-04-06 | 220 | +------+------+------------+--------------------+ 4 rows in set (0.02 sec)
参考链接
https://www.learnfk.com/mysql/mysql-in-clause.html
标签:06,Clause,typing,无涯,2007,MySQL,pages,+------+------+------------+--------------- From: https://blog.51cto.com/u_14033984/8588801