简体   繁体   English

具有多个连接和条件的Mysql查询

[英]Mysql query with multiple joins and conditions

I am trying to connect four tables using joints,here i used left join to connect tables and my condition is all the goods,item should be same and all the site should be same.same site have multiple goods, so i want to get the sum number of goods from each table. 我正在尝试使用关节连接四个表,这里我使用左连接来连接表,我的条件是所有商品,项目应该是相同的,所有的网站应该是相同的。网站有多个商品,所以我想得到每张桌子的货物总数。 my query is given 我的询问给出了

select 
    a.goods
    ,sum(a.no_of_units) as totala
    , a.site
    ,b.item
    ,sum(b.quantity) as totalb
    ,b.site
    ,c.goods
    ,c.site
    ,sum(c.no_of_units) as totalc
    ,d.site
    ,d.goods
    ,sum(d.quantity)b as totald 
from 
    inward_stock a 
left join 
    opening_balance b 
on 
    a.site=b.site 
and 
    a.goods=b.item 
left join 
    return_stock c 
on 
    b.site=c.site 
and 
    b.item=c.goods 
left join 
    stock_consumed d 
on 
    d.site=c.site
and 
    d.goods=c.goods

Can you put your conditions at the end of the joints like this: 你可以把你的条件放在关节的末端,如下所示:

 select a.goods,sum(a.no_of_units) as totala, a.site,b.item,sum(b.quantity) as totalb,b.site,c.goods,c.site,sum(c.no_of_units) as totalc,d.site,d.goods,sum(d.quantity) as totald from inward_stock a left join opening_balance b on (a.site=b.site) left join return_stock c on (b.site=c.site) left join stock_consumed d on (d.site=c.site) where (a.goods=b.item) and (b.item=c.goods) and (d.goods=c.goods) 

I have not tested your request but it seems that it's not bad. 我没有测试过您的请求,但似乎并不坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM