左加入 mysql 数据库
left join mysql database
我有 3 tables(产品,bill_Details 和账单),我想检索 bill_details table 中每个产品的数量,下面的查询只是给我 bill_details table "just" 中的产品,如果 bill_details 为空,则没有任何检索结果!
这是我的查询:
select p.prod_Id,p.prod_Name,
sum(b.de_Quantity+b.de_Bonus) - sum(bbb.de_Quantity+bbb.de_Bonus),
p.prod_Cost,p.prod_ExpDate,p.prod_BonusInfo,p.prod_Note
from
(((products p left JOIN bill_Details b on p.prod_Id=b.prod_Id)
left JOIN bill_Details bbb on p.prod_Id=bbb.prod_Id )
left JOIN bills a on b.bill_Id = a.bill_Id)
left JOIN bills aaa on bbb.bill_Id = aaa.bill_Id
where
a.cus_Sup=1 and aaa.cus_Sup=0 and a.archived=0 and aaa.archived=0
group by p.prod_Id,p.prod_Name,p.prod_Cost,p.prod_ExpDate,
p.prod_BonusInfo,p.prod_Note order by p.prod_Name asc;
您的 WHERE
子句正在检查左联接的表。通过这样做,您可以有效地删除左连接中不匹配的行。可能不是你想要的。
相反,您想要的可能是在连接子句本身中移动这些条件,如下所示:
select p.prod_Id,p.prod_Name,
sum(b.de_Quantity+b.de_Bonus) - sum(bbb.de_Quantity+bbb.de_Bonus),
p.prod_Cost,p.prod_ExpDate,p.prod_BonusInfo,p.prod_Note
from
(((products p left JOIN bill_Details b on p.prod_Id=b.prod_Id)
left JOIN bill_Details bbb on p.prod_Id=bbb.prod_Id )
left JOIN bills a on b.bill_Id = a.bill_Id and a.cus_Sup=1 and a.archived=0)
left JOIN bills aaa on bbb.bill_Id = aaa.bill_Id and aaa.cus_Sup=0 and aaa.archived=0
group by p.prod_Id,p.prod_Name,p.prod_Cost,p.prod_ExpDate,
p.prod_BonusInfo,p.prod_Note order by p.prod_Name asc;
我有 3 tables(产品,bill_Details 和账单),我想检索 bill_details table 中每个产品的数量,下面的查询只是给我 bill_details table "just" 中的产品,如果 bill_details 为空,则没有任何检索结果!
这是我的查询:
select p.prod_Id,p.prod_Name,
sum(b.de_Quantity+b.de_Bonus) - sum(bbb.de_Quantity+bbb.de_Bonus),
p.prod_Cost,p.prod_ExpDate,p.prod_BonusInfo,p.prod_Note
from
(((products p left JOIN bill_Details b on p.prod_Id=b.prod_Id)
left JOIN bill_Details bbb on p.prod_Id=bbb.prod_Id )
left JOIN bills a on b.bill_Id = a.bill_Id)
left JOIN bills aaa on bbb.bill_Id = aaa.bill_Id
where
a.cus_Sup=1 and aaa.cus_Sup=0 and a.archived=0 and aaa.archived=0
group by p.prod_Id,p.prod_Name,p.prod_Cost,p.prod_ExpDate,
p.prod_BonusInfo,p.prod_Note order by p.prod_Name asc;
您的 WHERE
子句正在检查左联接的表。通过这样做,您可以有效地删除左连接中不匹配的行。可能不是你想要的。
相反,您想要的可能是在连接子句本身中移动这些条件,如下所示:
select p.prod_Id,p.prod_Name,
sum(b.de_Quantity+b.de_Bonus) - sum(bbb.de_Quantity+bbb.de_Bonus),
p.prod_Cost,p.prod_ExpDate,p.prod_BonusInfo,p.prod_Note
from
(((products p left JOIN bill_Details b on p.prod_Id=b.prod_Id)
left JOIN bill_Details bbb on p.prod_Id=bbb.prod_Id )
left JOIN bills a on b.bill_Id = a.bill_Id and a.cus_Sup=1 and a.archived=0)
left JOIN bills aaa on bbb.bill_Id = aaa.bill_Id and aaa.cus_Sup=0 and aaa.archived=0
group by p.prod_Id,p.prod_Name,p.prod_Cost,p.prod_ExpDate,
p.prod_BonusInfo,p.prod_Note order by p.prod_Name asc;