MySQL的左联接不拉结果

mckeegan375

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

mysqli_query("SELECT product, expirydate, SUM(quantity), status FROM
   stockmovement a LEFT JOIN (SELECT  productid, product AS productname FROM
   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 ASC");

一切正常,除左联接外,当我尝试输出“产品名”时返回空白。有人可以看到查询出了什么问题吗?

在此先感谢您和圣诞快乐:)

拉胡尔

您的查询不应该像下面这样吗

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;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章