MySQL - clausola IN

Puoi usare IN clausola per sostituire molti OR condizioni

Capire IN clausola, considera un employee_tbl tabella, che contiene i seguenti record:

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)

Ora, supponiamo in base alla tabella sopra di voler visualizzare record con daily_typing_pages uguali a 250 e 220 e 170. Questo può essere fatto usando OR condizioni come segue

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)

Lo stesso può essere ottenuto utilizzando IN clausola come segue -

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)
mysql-funzioni-utili.htm