SQL 輸入
IN 運算子
IN 運算子可讓您在 WHERE 子句中規定多個值。
SQL IN 語法
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
FROM table_name
WHERE column_name IN (value1,value2,...);
示範資料庫
在本教學中,我們將使用php 樣本資料庫。
下面是選自"Websites" 表的資料:
+----+--------------+--- ------------------------+-------+---------+
| id | name | url | alexa | country |
+----+----------+------------------------------------ --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA | 1 |
| 2 | 淘寶 | https://www.taobao.com/ | 13 | CN cn/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 201 https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+----------- ----------------+-------+---------+
| id | name | url | alexa | country |
+----+----------+------------------------------------ --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA | 1 |
| 2 | 淘寶 | https://www.taobao.com/ | 13 | CN cn/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 201 https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+----------- ----------------+-------+---------+
下面的SQL 語句選取name 為"Google" 或"php中文網" 的所有網站:
實例#SELECT * FROM Websites
WHERE name IN ('Google','php中文網');#執行輸出結果: