两张表:
表1: 水果表
id fruit_name
1 apple
2 banana
3 pear
表2:盘子表
plate_id  fruit_id
1             1
1             2             
2             1
2             2 
2             3
3             1
3             2
3             3
3             4第一个查询:查出盘子id,满足条件:盘子里的水果全在表1中出现。即查出1和2盘
第二个查询:查出盘子id,满足条件:表一中的水果全在盘子中出现。即查出2和3盘。

解决方案 »

  1.   

    实现1
    select distinct a.plate_id from plate a ,(
    select a.plate_id from plate a left join fruit b on a.fruit_id=b.fruit_id where b.fruit_id is null
    ) b
    where a.plate_id<>b.plate_id实现2
    select plate_id from 
    (select b.plate_id,count(*) cnt from fruit a inner join plate b on a.fruit_id=b.fruit_id group by b.plate_id) a,
    (select count(*)cnt from fruit ) b
    where a.cnt>=b.cnt
      

  2.   

    第一个查询:查出盘子id,满足条件:盘子里的水果全在表1中出现。即查出1和2盘
    select distinct plate_id
    from 盘子表 x
    where not exists (select 1 from 盘子表 where fruit_id not in (select id from 水果表))
    第二个查询:查出盘子id,满足条件:表一中的水果全在盘子中出现。即查出2和3盘。
    select distinct plate_id
    from 盘子表 x
    where not exists (select 1 from 水果表 where id not in (select fruit_id from 盘子表 where plate_id=x.plate_id))