MySQL Relational Division

前端 未结 1 793
南旧
南旧 2021-01-23 05:52

I am having difficulties to solve one exercise:

For which People there is a Restaurant, that serves ALL their favorite beers.

(Yes, we actually have this in scho

相关标签:
1条回答
  • 2021-01-23 06:22

    Give this a try:

    SELECT DISTINCT fb1.name, fb1.surname FROM favoriteBeer fb1
    JOIN stock s ON fb1.beerName = s.beerName
    GROUP BY fb1.name, fb1.surname, s.restaurant
    HAVING COUNT(*) = (
      SELECT COUNT(*) FROM favoriteBeer fb2
      WHERE fb1.name = fb2.name AND fb1.surname = fb2.surname
    )
    

    Output:

    |        NAME |   SURNAME |
    |-------------|-----------|
    |      Bucher |      Rolf |
    | Mastroyanni |      Pepe |
    |       Meier |      Hans |
    |       Meier | Hanspeter |
    |       Meier | Hansruedi |
    |      Müller |  Heinrich |
    |      Peters |     Peter |
    |       Zarro |   Darween |
    

    Fiddle here.

    0 讨论(0)
提交回复
热议问题