Joining 3 tables SQL

227 Views Asked by At

I am trying to solve this problem for quite some time and the output is wrong. Can anyone help me with this? I am using mysql, so full join doesnt work. Thank yyou

3 tables:

Frequents (attributes: drinker, bar, times_a_week),
Likes (attributes: drinker, beer),
Serves (attributes: bar, beer, price),

The question ask for all the drinkers who frequent "every" bar which serve some beers they like

My answer looks like this:

SELECT drinker 
FROM frequents 
WHERE drinker NOT IN (SELECT f.drinker FROM frequents f 
                  JOIN likes l ON f.drinker=l.drinker
                  LEFT JOIN serves s ON l.beer=s.beer
                  AND s.bar=f.bar
                  WHERE s.bar IS NULL)
1

There are 1 best solutions below

0
On

how about this:

select * 
from Frequents
where bar in (select bar from Serves  S join Likes  L on S.beer=l.beer)

if it doesn't work, can you post some example of your data?