How to find all pizzerias that serve every pizza eaten by people over 30?

后端 未结 9 1970
被撕碎了的回忆
被撕碎了的回忆 2020-12-29 07:29

I\'m following the Stanford Database course and there\'s a question where we have Find all pizzerias that serve every pizza eaten by people over 30 using Re

9条回答
  •  醉梦人生
    2020-12-29 08:17

    Here is the conversion of http://oracletoday.blogspot.com/2008/04/relational-algebra-division-in-sql.html to MySQL

    
        mysql>create table parts (pid integer);
        mysql>create table catalog (sid integer,pid integer);
        mysql>insert into parts values ( 1), (2), (3), (4), (5);
        mysql>insert into catalog values (10,1);
    
    mysql>select * from catalog;
    +------+------+
    | sid  | pid  |
    +------+------+
    |   10 |    1 |
    |    1 |    1 |
    |    1 |    2 |
    |    1 |    3 |
    |    1 |    4 |
    |    1 |    5 |
    +------+------+
    
    
    mysql> select distict sid,pid from (select sid from catalog) a  join parts;
    +------+------+
    | sid  | pid  |
    +------+------+
    |   10 |    1 |
    |   10 |    2 |
    |   10 |    3 |
    |   10 |    4 |
    |   10 |    5 |
    |    1 |    1 |
    |    1 |    2 |
    |    1 |    3 |
    |    1 |    4 |
    |    1 |    5 |
    +------+------+
    
    
    mysql>select * from 
    (select distinct sid,pid from (select sid from catalog) a ,parts)  b where
    not exists (select 1 from catalog c where b.sid = c.sid and b.pid = c.pid);
    
    +------+------+
    | sid  | pid  |
    +------+------+
    |   10 |    2 |
    |   10 |    3 |
    |   10 |    4 |
    |   10 |    5 |
    +------+------+
    
    
    mysql>select distinct sid from catalog c1
    where not exists (
       select null from parts p
       where not exists (select null from catalog where pid=p.pid and c1.sid=sid));
    +------+
    | sid  |
    +------+
    |    1 |
    +------+
    
    

提交回复
热议问题