我今天刚刚看到这样一个问题。 我认为,这里的硬部分是,你重新寻找用户_id/profile_id组合,用户_id有每个团体_id_id_id_,至少没有。 因此,要加入通常的做法,并增加一些关联性,以计算每个群体/用户的分布情况,并确保其匹配(这几次已经编辑):
select user_id, profile_id
from user_groups join profile_groups on
user_groups.group_id=profile_groups.group_id
group by user_id, profile_id
having count(user_groups.group_id) =
(select count(*) from profile_groups as pg where
pg.profile_id=profile_groups.profile_id)
and count(profile_groups.group_id) = (select count(*) from user_groups as ug where
ug.user_id=user_groups.user_id)
;
这里的节目有两组,各有三组,其中一组是共同的,另一组是新用户。
sqlite> create table user_groups (user_id integer, group_id varchar);
sqlite> create table profile_groups (profile_id integer, group_id varchar);
sqlite> insert into user_groups values(1, group1 );
sqlite> insert into user_groups values(1, group2 );
sqlite> insert into user_groups values(1, group3 );
sqlite> insert into user_groups values(2, group1 );
sqlite> insert into user_groups values(2, group2 );
sqlite> insert into user_groups values(3, group4 );
sqlite> insert into user_groups values(4, group1 );
sqlite> insert into user_groups values(4, group5 );
sqlite> insert into user_groups values(4, group6 );
sqlite>
sqlite> insert into profile_groups values (11, group1 );
sqlite> insert into profile_groups values (11, group2 );
sqlite> insert into profile_groups values (11, group3 );
sqlite>
sqlite> insert into profile_groups values (21, group1 );
sqlite> insert into profile_groups values (21, group2 );
sqlite>
sqlite> insert into profile_groups values (22, group4 );
sqlite>
sqlite> insert into profile_groups values (23, group1 );
sqlite> insert into profile_groups values (23, group5 );
sqlite> insert into profile_groups values (23, group6 );
sqlite> select user_id, profile_id
...> from user_groups join profile_groups on
...> user_groups.group_id=profile_groups.group_id
...> group by user_id, profile_id
...> having count(user_groups.group_id) =
...> (select count(*) from profile_groups as pg where
...> pg.profile_id=profile_groups.profile_id)
...> and count(profile_groups.group_id) = (select count(*) from user_groups as ug where
...> ug.user_id=user_groups.user_id)
...> ;
1|11
2|21
3|22
4|23