表:
- 啤酒(名称,制造商)
- 酒吧(名称,地址,许可证)
- 出售(酒吧,啤酒,价格)
- 饮酒者(姓名,地址,电话)
- 喜欢(饮酒者,啤酒)
- 频繁(饮酒者,酒吧)
- 朋友(drinker1,drinker2)
任务:
查询#1:
SELECT x name
FROM Beers x, Drinkers y, Likes s
WHERE y.name=s.drinker and s.beer=x.name
GROUP BY x.name
HAVING COUNT(s.drinker) >= 2;
查询#2:
与之前的
相同查询#3。
SELECT x name
FROM Beers x, Friends y, Drinkers z, Likes p
WHERE z.name = "Anna"
AND z.name = y.drinker1
AND y.drinker2 = p.drinker
AND p.beer = x.name;
查询#4:
SELECT x.name
FROM Bars x, Sells y
WHERE x.name = y.bar
AND SOME(SELECT y.price) < ALL(SELECT t.price
FROM Sells t
WHERE t.bar = "99 bottles");
我对最后一个有点犹豫。我允许以这种方式使用SOME吗?我可以通过在select子句中使用外部变量来创建子查询吗?
答案 0 :(得分:1)
第3。找到安娜之友喜欢的所有啤酒:
您的答案有两个主要问题:
drinker1
或drinker2
列中这是一个使用显式JOIN
的示例解决方案:
SELECT x.name
FROM Beers
WHERE b.name IN (
SELECT l.beer
FROM Likes l
INNER JOIN Friends f
ON l.drinker = f.drinker1
WHERE f.drinker2 = "Anna"
UNION
SELECT l.beer
FROM Likes l
INNER JOIN Friends f
ON l.drinker = f.drinker2
WHERE f.drinker1 = "Anna"
)
<强> 4。找到所有出售啤酒的酒吧比“99瓶”酒吧销售的所有啤酒便宜:
您正在使用SOME和ALL;
SOME
或ALL
内的表达式必须是完整的子查询(SELECT y.price
不是有效的表达式)此处是使用IN
和MIN
的示例解决方案:
SELECT b.name
FROM Bars b
WHERE b.name IN (
SELECT s.bar
FROM Sells s
WHERE s.price <
(
SELECT MIN(price)
FROM Sells
WHERE bar = "99 bottles"
)
)