Re: [SQL ] update multiple-table 語法求教

看板Database (資料庫)作者 (汪汪)時間8年前 (2016/08/14 14:14), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/5 (看更多)
※ 引述《JeremyJoung (J.J.)》之銘言: : UPDATE `pb1` : JOIN `pb1` AS `pb2` ON pb1.pbin=pb2.pbin && pb2.pbon<>'' : SET pb1.opi=pb1.opi+pb2.opi : , pb1.oyd=pb1.oyd+pb2.oyd : , pb1.okg=pb1.okg+pb2.okg 感謝Jeremy大的回應, 不過有兩個小小的問題 第一次執行後的結果: +-----------+-----------+------+------+------+------+------+------+ | pbin | pbon | ipi | iyd | ikg | opi | oyd | okg | +-----------+-----------+------+------+------+------+------+------+ | 020417008 | | 51.0 | 6810 | 0.00 | 12.0 | 1601 | 0.00 | | 020417008 | 020502009 | 0.0 | 0 | 0.00 | 12.0 | 1601 | 0.00 | | 020417008 | 020518001 | 0.0 | 0 | 0.00 | 11.0 | 1401 | 0.00 | | 020417008 | 020611001 | 0.0 | 0 | 0.00 | 28.0 | 3808 | 0.00 | +-----------+-----------+------+------+------+------+------+------+ 只有加到第一筆(row:2) 第二次執行後的結果: +-----------+-----------+------+------+------+------+------+------+ | pbin | pbon | ipi | iyd | ikg | opi | oyd | okg | +-----------+-----------+------+------+------+------+------+------+ | 020417008 | | 51.0 | 6810 | 0.00 | 24.0 | 3202 | 0.00 | | 020417008 | 020502009 | 0.0 | 0 | 0.00 | 12.0 | 1601 | 0.00 | | 020417008 | 020518001 | 0.0 | 0 | 0.00 | 11.0 | 1401 | 0.00 | | 020417008 | 020611001 | 0.0 | 0 | 0.00 | 28.0 | 3808 | 0.00 | +-----------+-----------+------+------+------+------+------+------+ 一樣只加到第一筆(row:2),而且值會不斷的重複加上去 有辦法用sum(),或者在累加這三筆前先歸零嗎^^ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.241.224.25 ※ 文章網址: https://www.ptt.cc/bbs/Database/M.1471155256.A.EC9.html
文章代碼(AID): #1Ni0mux9 (Database)
文章代碼(AID): #1Ni0mux9 (Database)