【发布时间】:2014-06-17 07:07:38
【问题描述】:
我有两张桌子。我想从表 1 中选择 id 并从表 2 中计算相同的值
表 1
Id qId opt
1 30 Chris Christie
2 30 Hillary Clinton
3 30 Allan West
4 30 Joe Biden
5 31 Mark
6 31 Ben Johnson
表2
poll_id qId ansId
201 30 1
202 30 2
204 31 8
我尝试的以下查询仅输出 ansId 1 和 2,因为 Table2 中没有 3 和 4。
SELECT a.Id,
a.opt,
COUNT(b.ansId)
from Table1 a
INNER JOIN Table2 b ON a.Id = b.ansId
where a.qId =30
但我需要所有 ansId 1,2,3,4,计数为 3 和 4 为 0,如下所示。
Id opt COUNT(b.ansId)
1 Chris Christie 1
2 Hillary Clinton 1
3 Allan West 0
4 Joe Biden 0
【问题讨论】:
-
非常感谢朱奈德。突然在没有正确格式的情况下发布。
-
请查看更新后的表1。很抱歉。
标签: mysql count inner-join