一、案例展示
1数据准备
-- 创建语句
CREATE TABLE `tablea` (
`id` int(11) NOT NULL,
`num` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 插入语句
INSERT INTO tablea (id, num) VALUES(1, 1);
INSERT INTO tablea (id, num) VALUES(2, 1);
INSERT INTO tablea (id, num) VALUES(3, 1);
INSERT INTO tablea (id, num) VALUES(4, 2);
INSERT INTO tablea (id, num) VALUES(5, 3);
INSERT INTO tablea (id, num) VALUES(6, 3);
INSERT INTO tablea (id, num) VALUES(7, 3);
INSERT INTO tablea (id, num) VALUES(8, 2);
INSERT INTO tablea (id, num) VALUES(9, 2);
-- 查询语句
select
distinct num
from
tablea a
where
a.num =(select num from tablea where a.id=id-1)
and a.num =(select num from tablea where a.id=id-2);
2结果展示
执行sql后