Re: [請益] 如何做到好幾個資料表的資料聯集合併?

看板PHP作者 (.......................)時間16年前 (2009/09/09 13:29), 編輯推噓1(102)
留言3則, 1人參與, 最新討論串2/2 (看更多)
這個有點麻煩 我用 MySQL 測試, 包兩層 SQL select pno, pname, customer, type, cdate, sum(cnum), (select total from a where a.pno=tmp.pno) + ifnull( (select sum(Pnum) from c where c.Pdate <= tmp.cdate and c.pno=tmp.pno ),0) - ifnull( (select sum(Onum) from b where b.Odate <= tmp.cdate and b.pno=tmp.pno), 0) as tnum from ( select a.pno as pno, a.pname as pname, b.customer as customer, '訂購' as type, b.Odate as cdate, b.Onum as cnum from a inner join b on a.pno=b.pno union all select a.pno as pno, a.pname as pname, '總公司' as customer, '採購' as type, c.Pdate as cdate, c.Pnum as cnum from a inner join c on a.pno=c.pno ) as tmp group by tmp.pno, tmp.pname, tmp.customer, tmp.cdate order by tmp.pno, tmp.cdate 查詢結果 pno pname customer type cdate cnum tnum -------------------------------------------------------------------- A1234 白色包包 小馬 訂購 2009-08-22 4 -1 A1234 白色包包 總公司 採購 2009-08-25 2 1 A1234 白色包包 小明 訂購 2009-09-01 2 -1 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.171.135.37

09/09 22:47, , 1F
我會再試試看,當初想說好玩,所以幫朋友寫寫看
09/09 22:47, 1F

09/09 22:48, , 2F
想了很久還是有點繞不出來,這版上高手如雲,很多值
09/09 22:48, 2F

09/09 22:49, , 3F
得學習的,謝謝!
09/09 22:49, 3F
文章代碼(AID): #1Afpqb9_ (PHP)
文章代碼(AID): #1Afpqb9_ (PHP)