27,579
社区成员
发帖
与我相关
我的任务
分享
create table tb(ID int, keyword varchar(8))
insert tb select 1, 'k1'
union all select 2, 'k1'
union all select 3, 'k1'
union all select 2, 'k2'
union all select 3, 'k2'
union all select 4, 'k2'
select id from
(
select distinct id from tb where keyword = 'k1'
union all
select distinct id from tb where keyword = 'k2'
) t
group by id having count(*) = 2
drop table tb
/*
id
-----------
2
3
(所影响的行数为 2 行)
*/
create table tb(id int,keyword varchar(50))
insert into tb select 1,'k1'
insert into tb select 1,'k2'
insert into tb select 2,'k1'
insert into tb select 2,'k1'
insert into tb select 3,'k2'
insert into tb select 3,'k2'
select * from tb where id in(
select id from (
select distinct * from tb where keyword='k1'
union all
select distinct * from tb where keyword='k2')t
group by id having count(1)>1)
select id from
(
select distinct id from tb where keyword = 'k1'
union all
select distinct id from tb where keyword = 'k2'
) t
group by id having count(*) = 2
create table tb(ID int, keyword varchar(8))
insert tb select 1, 'k1'
union all select 2, 'k1'
union all select 3, 'k1'
union all select 2, 'k2'
union all select 3, 'k2'
union all select 4, 'k2'
select ID from tb where keyword='k1'
intersect
select ID from tb where keyword='k2'
/*
ID
-----------
2
3
(2 row(s) affected)
*/
drop table tb
select ID from tb where keyword='k1'
intersect
select ID from tb where keyword='k2'