SQL IN 操作符
SQL IN
操作符用于在 WHERE 子句中限制一列只能包含有限个值
SELECT column_name(s) FROM table_name WHERE column_name IN ( value1 ,value2 ,...);
演示数据
先在 MySQL 数据库运行下面的语句创建测试数据
CREATE DATABASE IF NOT EXISTS twle default character set utf8mb4 collate utf8mb4_unicode_ci; USE twle; DROP TABLE IF EXISTS `lession`; CREATE TABLE `lession` ( id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, name varchar(32) default '', views int(11) NOT NULL default '0', created_at DATETIME ); INSERT INTO lession(id,name,views,created_at) VALUES (1, 'Python 基础教程',981,'2017-04-18 13:52:03'), (2, 'Scala 基础教程',73,'2017-04-18 16:03:32'), (3, 'Ruby 基础教程',199,'2017-05-01 06:16:14'), (4, 'SQL 基础教程', 533,'2017-05-02 08:13:42');
使用 SELECT * FROM lession;
运行结果如下
mysql> select * from lession; +----+---------------------+-------+---------------------+ | id | name | views | created_at | +----+---------------------+-------+---------------------+ | 1 | Python 基础教程 | 981 | 2017-04-18 13:52:03 | | 2 | Scala 基础教程 | 73 | 2017-04-18 16:03:32 | | 3 | Ruby 基础教程 | 199 | 2017-05-01 06:16:14 | | 4 | SQL 基础教程 | 533 | 2017-05-02 08:13:42 | +----+---------------------+-------+---------------------+
总共有 4 条记录
IN 操作符
下面的 SQL 语句选取 id 为 "1" 或 "3" 的所有课程
SELECT * FROM lession WHERE id IN (1,3);
运行结果如下
mysql> SELECT * FROM lession WHERE id IN (1,3); +----+---------------------+-------+---------------------+ | id | name | views | created_at | +----+---------------------+-------+---------------------+ | 1 | Python 基础教程 | 981 | 2017-04-18 13:52:03 | | 3 | Ruby 基础教程 | 199 | 2017-05-01 06:16:14 | +----+---------------------+-------+---------------------+