MySQL Left Join没有拉入结果

时间:2016-12-24 19:12:16

标签: mysql left-join

我有一个MySQL查询,我试图在另一个表中根据它的ID在一个表中查找产品名称,如下所示:

select *
from messages m1
where time = greatest((
    select max(time)
    from messages m2
    where m2.from = m1.from
      and m2.to   = m1.to
),(
    select max(time)
    from messages m2
    where m2.from = m1.to
      and m2.to   = m1.from
))

当我尝试输出'productname'时,一切都与左连接不同,后者返回空白。任何人都可以看到查询出错的地方吗?

提前致谢并祝圣诞快乐:)

1 个答案:

答案 0 :(得分:2)

你的查询不应该像下面那样

SELECT a.product, a.expirydate, 
SUM(a.quantity), a.status, b.product as productname 
FROM stockmovement a 
LEFT JOIN products b ON a.product = b.productid 
WHERE a.status = '0' 
GROUP BY a.product, a.expirydate 
HAVING SUM(a.quantity) > 0 
ORDER BY a.product, a.expirydate;