我是mysql的新手,无法弄清楚如何加入这两个选择的结果。有什么建议吗?
select COUNT(COLORANTCODE) as Colorant_Count from Formula group by COLORANTCODE;
select COLORANTNAME from Colorant;
我所尝试的一切都没有效果。
Formula包含COLORANTCODE,它是包含COLORANTCODE和COLORANTNAME的Colorant的FK。
我需要结果表包含COLORANTNAME以及它在公式中显示的次数。
答案 0 :(得分:1)
利用LEFT JOIN
SELECT c.colorantname, COALESCE(COUNT(f.colorantcode), 0) count
FROM colorant c LEFT JOIN Formula f
ON c.colorantcode = f.colorantcode
GROUP BY c.colorantcode, c.colorantname
推荐阅读:
答案 1 :(得分:0)
Select COLORANTCODE, Count(*) as Colorant_count
from Formula
Group by COLORANTCODE