MySQL之IN的用法

可以使用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)


http://www.yiibai.com/mysql/mysql_in_clause.html

http://zhidao.baidu.com/link?url=L2QbNVi8LCyirLLWmVIQLH4hro23Ia7dN2LqY5T5PMlnd8WtxxyJH72G40EOElFdrSh8kTnA1M_2VAXpV6X6R_


你可能感兴趣的:(Database,&,Structure)