SQL IN 语法
IN 操作符允许您在 WHERE 子句中规定多个值。
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
演示数据库
在猿变手册教程中,我们将使用 yuanbian 样本数据库。
下面是选自 "articles" 表的数据:
+----+--------------+----------+--------+---------------------+
| id | title | author | a_type | pub_date |
+----+--------------+---------—+--------+---------------------+
| 1 | hadoop简介 | 大猿 | 311 | 2003-01-05 11:23:00 |
| 2 | python元类 | 小猿 | 163 | 2003-01-11 13:00:01 |
| 3 | HTML基础 | 猿大头 | 497 | 2004-03-09 12:00:00 |
| 4 | JavaScript | 猿媛 | 20 | 2005-11-3 17:34:12 |
| 5 | ETL快速入门 | 猿媛 | 3 | 2005-11-7 18:40:00 |
+----+--------------+----------+--------+---------------------+
IN 操作符示例
下面的 SQL 语句选取 id 为 1、3、5 的所有文章:
SELECT * FROM articles
WHERE id IN (1, 3, 5);
执行输出结果:
+----+--------------+----------+--------+---------------------+
| id | title | author | a_type | pub_date |
+----+--------------+---------—+--------+---------------------+
| 1 | hadoop简介 | 大猿 | 311 | 2003-01-05 11:23:00 |
| 3 | HTML基础 | 猿大头 | 497 | 2004-03-09 12:00:00 |
| 5 | ETL快速入门 | 猿媛 | 3 | 2005-11-7 18:40:00 |
+----+--------------+----------+--------+---------------------+
讨论区