亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用LEFT?JOIN?統(tǒng)計(jì)左右存在的數(shù)據(jù)問(wèn)題

 更新時(shí)間:2022年10月25日 09:00:47   作者:小碼code  
最近做了一個(gè)數(shù)據(jù)模塊的統(tǒng)計(jì),統(tǒng)計(jì)企業(yè)收款、發(fā)票相關(guān)的數(shù)據(jù),開(kāi)始統(tǒng)計(jì)是比較簡(jiǎn)單,后面再拆分賬套統(tǒng)計(jì)就有點(diǎn)小復(fù)雜,這篇文章主要介紹了使用LEFT?JOIN?統(tǒng)計(jì)左右存在的數(shù)據(jù),需要的朋友可以參考下

最近做了一個(gè)數(shù)據(jù)模塊的統(tǒng)計(jì),統(tǒng)計(jì)企業(yè)收款、發(fā)票相關(guān)的數(shù)據(jù),開(kāi)始統(tǒng)計(jì)是比較簡(jiǎn)單,后面再拆分賬套統(tǒng)計(jì)就有點(diǎn)小復(fù)雜,本文做一個(gè)簡(jiǎn)單的記錄。

需求

企業(yè)表

企業(yè)表t_company有如下字段:標(biāo)識(shí)id、企業(yè)名稱name:

idname
1騰訊
2百度

收款表

企業(yè)對(duì)應(yīng)有收款表t_collection有如下字段:標(biāo)識(shí)id、賬套account、企業(yè)idcompany_id、收款金額amount

idaccountcompany_idamount
11130
22120
31230
42240

開(kāi)票表

開(kāi)票表t_invoice有如下字段:標(biāo)識(shí)id、賬套account、企業(yè)idcompany_id、發(fā)票金額amount

idaccountcompany_idamount
11110
22120
31230
42250

匯總企業(yè)統(tǒng)計(jì)

現(xiàn)在要做一個(gè)統(tǒng)計(jì),統(tǒng)計(jì)企業(yè)收款金額,以及發(fā)票金額,需要將收款表和發(fā)票表將company_idgroup up操作。開(kāi)票表也是做類似的操作,企業(yè)表和上面的結(jié)果做left join連接操作,sql如下:

select tc.id,tc.name,tc2.amount as collection_amount,ti.amount as invoice_amunt from t_company tc 
left join (
  select company_id,sum(amount) as amount from t_collection group by company_id
) tc2 on tc.id = tc2.company_id
left join (
  select company_id,sum(amount) as amount from t_invoice group by company_id
) ti on tc.id = ti.company_id

查詢結(jié)果:

idnamecollection_amountinvoice_amunt
1騰訊5030
2百度7080

再分賬套做匯總(重點(diǎn))

在上面統(tǒng)計(jì)的基礎(chǔ)上,再拆分賬套統(tǒng)計(jì)。

收款表和發(fā)票表做賬套的拆分,和企業(yè)表做關(guān)聯(lián):

select tc.id,tc.name,tc2.amount as collection_amount,ti.amount as invoice_amunt from t_company tc 
left join (
  select company_id,account,sum(amount) as amount from t_collection 
  group by company_id,account
) tc2 on tc.id = tc2.company_id
left join (
  select company_id,account,sum(amount) as amount from t_invoice 
  group by company_id,account
) ti on tc.id = ti.company_id and tc2.account = ti.account

首先是將收款表做賬套的拆分,然后關(guān)聯(lián)發(fā)票表的賬套拆分??此茮](méi)有問(wèn)題,但是left join返回左邊的所有記錄,以及右邊字段相等的數(shù)據(jù)。

這樣就有一個(gè)問(wèn)題:

如果左邊表沒(méi)有的數(shù)據(jù),右邊的表也不會(huì)查出來(lái)。比如以上查詢收款表不存在的賬套,發(fā)票表存在賬套也不會(huì)查出來(lái)。這就是left join的局限性。

全表連接解決方案一:

MySQLleft join、right join應(yīng)該也有full join全表連接。

但是MySQL是不支持full join全表連接。

網(wǎng)上也有解決方案使用union替換full_join,思路是左表左連接右邊,左表右連接右邊,將上面的兩個(gè)結(jié)果union連接起來(lái):

select * from t1 left join t2 on t1.id = t2.id
union 
select * from t1 right join t2 on t1.id = t2.id;

上面只是兩個(gè)表的關(guān)聯(lián),如果三個(gè)表或者更多的關(guān)聯(lián),寫(xiě)起來(lái)就比較繁瑣了。

全表連接解決方案二:

全表連接就是一個(gè)沒(méi)有限制的左表連接,就是去掉on關(guān)聯(lián)條件,

left join所有的賬套,首先要顯示全所有的賬套,企業(yè)表關(guān)聯(lián)賬套表,但是兩個(gè)表是沒(méi)有關(guān)聯(lián)的,需要去掉on后面的關(guān)聯(lián)條件,但是MySQL語(yǔ)法連接后面必須要加on,將約束條件改成1 = 1即可:

 select tc.id,tc.name,ta.id as account from t_company tc left join t_account ta on 1 = 1
idnameaccount
1騰訊1
1騰訊2
2百度1
2百度2

查詢出所有的公司賬套之后,再left join收款表和發(fā)票表:

select tc.id,tc.name,tc.account,tc2.amount as collection_amount,ti.amount as invoice_amunt from (
select tc.id,tc.name,ta.id as account from t_company tc left join t_account ta on 1 = 1
)tc
left join (
  select company_id,account,sum(amount) as amount from t_collection group by company_id,account
) tc2 on tc.id = tc2.company_id and tc.account = tc2.account
left join (
  select company_id,account,sum(amount) as amount from t_invoice group by company_id,account
) ti on tc.id = ti.company_id and tc.account = ti.account

結(jié)果:

idnameaccountcollection_amountinvoice_amunt
1騰訊13010
1騰訊22020
2百度13030
2百度24050

總結(jié)

  • 企業(yè)分組統(tǒng)計(jì)收款和發(fā)票表,只需要對(duì)企業(yè)做group by分組即可。
  • 企業(yè)和賬套一起分組,left join只會(huì)統(tǒng)計(jì)左邊存在的數(shù)據(jù),而需要統(tǒng)計(jì)兩邊都存在的數(shù)據(jù)。
    • 使用union多表查詢比較繁瑣。
    • left join使用on 1 = 1查詢不添加限制條件,查詢所有公司的賬套,再關(guān)聯(lián)發(fā)票和收款。

參考

到此這篇關(guān)于使用LEFT JOIN 統(tǒng)計(jì)左右存在的數(shù)據(jù)的文章就介紹到這了,更多相關(guān)left join左右存在的數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論