第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何對(duì) 3 個(gè)表中的每個(gè)項(xiàng)目求和

如何對(duì) 3 個(gè)表中的每個(gè)項(xiàng)目求和

PHP
侃侃爾雅 2022-09-17 15:46:32
我有三張桌子:面積,人,余額明細(xì)面積:Code  AreaName0001  A 0002  B0003  C0004  D人:id code personid personname customername customernumber1  0001 1000     test1      loop         50002  0001 1000     test1      loop         70003  0002 1001     test2      loop2        60004  0003 1002     test3      loop3        60055  0001 1000     test1      loop5        6100余額詳情 :id period       customernumber balance1  2019-12-31   5000           502  2019-12-31   6000           1503  2019-12-31   6005           1404  2019-12-31   6100           405  2019-12-31   7000           206  2020-01-17   5000           1507  2020-01-17   6000           408  2020-01-24   6100           1809  2020-01-24   6000           170我想根據(jù)每個(gè)項(xiàng)目的區(qū)號(hào)和時(shí)間段獲得余額。我使用了以下查詢$query= $con->query("SELECT * FROM Area WHERE Code NOT IN ('0004') GROUP BY AreaName");while ($row = $query->fetch_array()) {     $balancequery = $con->query("SELECT SUM(BalanceDetail.balance) as balance FROM Person JOIN BalanceDetail ON Person.customernumber= BalanceDetail.customernumber WHERE Person.code= '$row[Code]' AND period='2019-12-31'");     $balancequery2 = $con->query("SELECT SUM(BalanceDetail.balance) as balance FROM Person JOIN BalanceDetail ON Person.customernumber= BalanceDetail.customernumber WHERE Person.code= '$row[Code]' AND period='2020-01-17'");     $balancequery3 = $con->query("SELECT SUM(BalanceDetail.balance) as balance FROM Person JOIN BalanceDetail ON Person.customernumber= BalanceDetail.customernumber WHERE Person.code= '$row[Code]' AND period='2020-01-24'");}我想把它組合成一個(gè)sql,所以在 while 循環(huán)中不會(huì)有查詢
查看完整描述

3 回答

?
冉冉說(shuō)

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊

您可以使用條件聚合來(lái)解決此問(wèn)題,只需對(duì)每個(gè)周期的相關(guān)值求和:


SELECT a.code,

       SUM(CASE WHEN period='2019-12-31' THEN b.balance ELSE 0 END) AS `balance 2019-12-31`,

       SUM(CASE WHEN period='2020-01-17' THEN b.balance ELSE 0 END) AS `balance 2020-01-17`,

       SUM(CASE WHEN period='2020-01-24' THEN b.balance ELSE 0 END) AS `balance 2020-01-24`

FROM Area a

JOIN Person p ON p.code = A.code

JOIN BalanceDetail b ON b.customernumber = p.customernumber 

GROUP BY a.code

輸出:


code    balance 2019-12-31  balance 2020-01-17  balance 2020-01-24

1       110                 150                 180

2       150                 40                  170

3       140                 0                   0


查看完整回答
反對(duì) 回復(fù) 2022-09-17
?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以使用分組依據(jù)


SELECT Person.Code, BalanceDetail.period, SUM(BalanceDetail.balance) as balance 

FROM Person JOIN BalanceDetail 

ON Person.customernumber= BalanceDetail.customernumber

WHERE Code IN (SELECT Code  FROM Area WHERE Code NOT IN ('0004'))

group by Person.Code, BalanceDetail.period


查看完整回答
反對(duì) 回復(fù) 2022-09-17
?
長(zhǎng)風(fēng)秋雁

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊

SELECT  bd.period,

        a.AreaName,

        SUM(bd.balance) as balance

    FROM Person AS p

    JOIN BalanceDetail AS bd ON p.customernumber = bd.customernumber

    JOIN Area AS a  ON a.code = p.code

    GROUP BY bd.period, p.code;


查看完整回答
反對(duì) 回復(fù) 2022-09-17
  • 3 回答
  • 0 關(guān)注
  • 136 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)