我有两个表:列表和标签。这些是样本记录:
SELECT l.tag,
count(case when l.type = 'Consumer' then l.type = '' end) as consumer,
count(case when l.type = 'Supplier' then l.type = '' end) as supplier
FROM listings AS l
LEFT JOIN tags AS t
ON l.tag = t.tag
GROUP BY t.tag, l.type
任何想法怎么做?我很乐意感谢任何帮助。谢谢。
更新:已更改为:
SELECT l.tag,
count(case when l.type = 'Consumer' then l.type = '' end) as consumer,
count(case when l.type = 'Supplier' then l.type = '' end) as supplier
FROM listings AS l
LEFT JOIN tags AS t
ON l.tag = t.tag
GROUP BY t.tag, l.type
OUTER 关键字似乎有效,但我需要为消费者和供应商显示AZ为0和0。
答案 0 :(得分:1)
为什么在保存标记名称本身而不是列表表格中的ID时使用join来获取此内容。
您可以使用此查询
SELECT tag,
SUM(CASE when type = 'Consumer' then 1 else 0 END) AS Consumer,
SUM(CASE when type = 'Supplier' then 1 else 0 END) AS Supplier
FROM `listings` group by tag
答案 1 :(得分:1)
试试这个...........
SELECT t.tag,
count(case when l.type = 'Consumer' then l.type = '' end) as consumer,
count(case when l.type = 'Supplier' then l.type = '' end) as supplier
FROM listings AS l
RIGHT JOIN tags AS t
ON l.tag = t.tag
GROUP BY t.tag