发布于 2015-07-31 16:35:12 | 341 次阅读 | 评论: 0 | 来源: 网络整理

可以使用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)
 
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务