【问题标题】:Mysql display records even no resultmysql显示记录竟然没有结果
【发布时间】:2017-11-21 10:06:58
【问题描述】:

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

房源表

标签表

这是我的示例 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

但它显示如下:


我想要这样的东西:

任何想法如何做到这一点?我很乐意为您提供任何帮助。谢谢。

更新:改为:

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 关键字似乎有效,但我仍需要为消费者和供应商显示 0 和 0 的 AZ。

【问题讨论】:

  • @Epodax 错误地添加了 php.ini 文件。更改为 SQL。泰。
  • 你有select distinct l.taggroup by t.tag l.tag = t.tag加入。所以实际上你是在要求它显示一个独特列的不同单位摆脱不同的。
  • @Martin 已删除 Distinct 但结果仍然相同。
  • l.tag 而不是t.tag 分组我怀疑这不会有帮助,但它会保留listings 表上的所有工作,而不是跨两个表?

标签: mysql sql


【解决方案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

【讨论】:

【解决方案2】:

当您保存标签名称而不是列表表中的 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

【讨论】:

  • 谢谢。我需要加入表格是有原因的(客户的要求)您的查询似乎有效,但是它没有显示 AZ 为消费者和供应商的 0 和 0
猜你喜欢
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多