18、SQL IN 操作符
SQLIN 操作符用于在 WHERE 子句中限制一列只能包含有限个值
SELECT column_name(s) FROM table_name WHERE column_name IN ( value1 ,value2 ,...);
演示数据
先在 MySQL 数据库运行下面的语句创建测试数据
CREATE DATABASE IF NOT EXISTS ddkk default character set utf8mb4 collate utf8mb4_unicode_ci;
USE ddkk;
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 DDKK.COM 弟弟快看',981,'2017-04-18 13:52:03'),
(2, 'Scala DDKK.COM 弟弟快看',73,'2017-04-18 16:03:32'),
(3, 'Ruby DDKK.COM 弟弟快看',199,'2017-05-01 06:16:14'),
(4, 'SQL DDKK.COM 弟弟快看', 533,'2017-05-02 08:13:42');
使用SELECT * FROM lession; 运行结果如下
mysql> select * from lession;
+----+---------------------+-------+---------------------+
| id | name | views | created_at |
+----+---------------------+-------+---------------------+
| 1 | Python DDKK.COM 弟弟快看 | 981 | 2017-04-18 13:52:03 |
| 2 | Scala DDKK.COM 弟弟快看 | 73 | 2017-04-18 16:03:32 |
| 3 | Ruby DDKK.COM 弟弟快看 | 199 | 2017-05-01 06:16:14 |
| 4 | SQL DDKK.COM 弟弟快看 | 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 DDKK.COM 弟弟快看 | 981 | 2017-04-18 13:52:03 |
| 3 | Ruby DDKK.COM 弟弟快看 | 199 | 2017-05-01 06:16:14 |
+----+---------------------+-------+---------------------+