【问题标题】:mysql query join display 0 valuemysql查询连接显示0值
【发布时间】:2020-08-04 00:48:51
【问题描述】:

我需要像这样显示关系表的总和: enter image description here

这是我的查询:

select p.p_kode, p.name as p_name, sum(m.money) as jum
from person p
left join money as m on m.p_kode = p.`p_kode`
where m.date >= "2020-04-10" and m.date <= "2020-04-13"
group by p.p_kode

但还是不喜欢我想要的。 K003 仍然没有显示jum = 0 值。 请帮忙。谢谢!

【问题讨论】:

  • 我认为您需要将p.name逐段添加到组中
  • 将条件 m.date &gt;= "2020-04-10" and m.date &lt;= "2020-04-13" 移至 ON 子句。
  • @MahdyAslamy 仍然无法正常工作
  • @forpas 应该是怎样的?
  • on m.p_kode = p.p_kode and m.date &gt;= "2020-04-10" and m.date &lt;= "2020-04-13"

标签: mysql sql join group-by left-join


【解决方案1】:

只需将条件从where 子句移动到left joinon 部分 - 否则,条件变为强制性,并从person 逐出行,left join 为空返回:

select p.p_kode, p.name as p_name, coalesce(sum(m.money), 0) as jum
from person p
left join money as m 
    on  m.p_kode = p.p_kode
    and m.date >= '2020-04-10' 
    and m.date <= '2020-04-13'
group by p.p_kode

【讨论】:

    猜你喜欢
    • 2014-05-26
    • 2012-12-28
    • 1970-01-01
    • 2015-07-24
    • 2015-03-10
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多