A,B的交集?
select val from t1,t2 where t2id=t2.id 
and name in('A','B')
group  by val having count(val)>1 
A,B,C的交集?
select val from t1,t2 where t2id=t2.id 
and name in('A','B','C')
group  by val having count(val)>1 任意指定集合
改变name in 的集合即可要想函数一样调用需存储过程
你用什么数据库

解决方案 »

  1.   

    两个可以用 A,B,C的交集 错了
    想偷懒没成功select val from t1,t2 where t2id=t2.id and t2.name='C'
    and val in
    (select val from t1,t2 where t2id=t2.id 
    and name in('A','B')
    group  by val having count(val)>1 )
      

  2.   

    select distinct val from t1 where val in (select distinct val from t1 a inner join t2 b on a.id=b.id where b.name='A') and val in (select distinct val from t1 a inner join t2 b on a.id=b.id where b.name='B')//AB交集select distinct val from t1 where val in (select distinct val from t1 a inner join t2 b on a.id=b.id where b.name='A') and val in (select distinct val from t1 a inner join t2 b on a.id=b.id where b.name='B') and val in (select distinct val from t1 a inner join t2 b on a.id=b.id where b.name='C')//ABC交集