Mysql显示记录甚至没有结果

时间:2017-06-19 09:05:51

标签: mysql sql

我有两个表:列表和标签。这些是样本记录:

列表
enter image description here
标签表
enter image description here
这是我的示例SQL查询:

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

但它显示如下:
enter image description here


我想要这样的事情:
enter image description here

任何想法怎么做?我很乐意感谢任何帮助。谢谢。

更新:已更改为:

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


这就是结果:
enter image description here

OUTER 关键字似乎有效,但我需要为消费者和供应商显示AZ为0和0。

2 个答案:

答案 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