文章詳情頁
mysql sum去除重復(fù)
瀏覽:104日期:2022-06-20 11:32:53
問題描述
A表
id name price1 a 12 b 1
B表
id type1hh1hh2ccselect sum(price) from A as a left join B as b on a.id=b.id where b.type=hh返回結(jié)果2,現(xiàn)在我想根據(jù)A表id(distinct)統(tǒng)計,得出sum(price)為1的sql語句怎么寫?
問題解答
回答1:B表兩條id為1的記錄,left join查詢會得到兩條記錄并求和,所以就為2了,你在B表中刪除一條id為1的數(shù)據(jù),應(yīng)該就對了。但是問題并沒有解決,可以這樣:
select sum(price) from A as a left join (select id,type from B group by id) as temp on a.id=temp.id where temp.type=hh
由于沒有任何開發(fā)環(huán)境,沒法測試,大致就這樣吧。
上一條:mysql - Access denied for user ’ODBC’@’localhost’ (using password: NO)下一條:mysql - 如何聯(lián)合查詢兩個結(jié)構(gòu)相同的表并且id不重復(fù)
相關(guān)文章:
1. dockerfile - 我用docker build的時候出現(xiàn)下邊問題 麻煩幫我看一下2. android-studio - Android studio導(dǎo)入老版本的2048項目報錯3. TP6.0 Jump.php報錯4. 為什么return不了數(shù)據(jù)給ajax5. 請問一下各位老鳥 我一直在學(xué)習獨孤九賤 現(xiàn)在是在tp5 今天發(fā)現(xiàn) 這個系列視頻沒有實戰(zhàn)6. 急急急?。。∏蟠笊窠獯鹁W(wǎng)站評論問題,有大神幫幫小弟嗎7. php mail無法發(fā)送郵件8. PHP類屬性聲明?9. PHP 怎么api獲取json數(shù)據(jù)10. 為什么要通過常量的方式拐彎抹角的寫呢,直接寫DSN之類的不好嗎
排行榜

熱門標簽