我有3张桌子:
因此,我必须以最少的艺术家数量找到每种类型艺术品的主题和艺术家数量。
我写了以下内容,但显然它不起作用。有人能帮助我吗?
SELECT o.subject, COUNT(au.autor) count
FROM Artistic au, Art o
WHERE o.oid=au.autor
GROUP BY o.type
HAVING count = (SELECT MIN(count) FROM Artistic au);
答案 0 :(得分:0)
只使用计算列,而不是ALIAS。
使用正确的连接语法
SELECT o.subject,
COUNT(au.autor) count
FROM Artistic au
INNER JOIN Art o
ON o.oid = au.autor
GROUP BY o.type, o.subject
HAVING COUNT(au.autor) = (SELECT MIN(count) FROM Artistic au)
答案 1 :(得分:0)
你的问题是:“我必须找到每种类型艺术家的艺术家的主题和艺术家人数”。
这种类型的查询在MySQL中有点痛苦。您需要按type
和subject
进行汇总,然后选择具有最小计数的subject
。这需要额外的join
操作:
SELECT ts.`type`, ts.subject, ts.count
FROM (SELECT a.`type`, a.subject, COUNT(au.autor) as `count`
FROM Artistic au join
Art a
on a.oid = au.autor
GROUP BY a.`type`, a.subject
) ts join
(select a.`type`, min(`count`) as mincount
from (SELECT a.type, a.subject, COUNT(au.autor) as `count`
FROM Artistic au join
Art a
on a.oid = au.autor
GROUP BY a.type, a.subject
) ts
group by a.`type`
) t
on t.`type` = ts.`type`
ORDER BY `count` desc;