[英]Is this the right way to Select distinct from table with mySql?
我想總結一下userdd與我的圖像不同的“grundfrakt”。 所以我想只為每個userId和ordernummer加一個值。
我得到值115,這個選擇查詢是正確的。 而我只是想絕對確定我是以正確的方式做到的? 所以我不會錯過任何可能搞砸的東西!?
SELECT SUM(DISTINCT grundfrakt) FROM cart where ordernummer='176748409';
謝謝!
新問題!
謝謝The Impaler,它很棒。 但是,如果“grundfrakt”在另一個表(用戶)中,你怎么能這樣做呢? 再次感謝。
所以我有“購物車”和“用戶”表,我希望從用戶表中獲得獨特的“grundfrakt”。
假設grundfrakt
總是具有與userid
/ ordernummer
組合完全相同的值,那么您需要的查詢是:
select
sum(x.m)
from (
select max(grundfrakt) as m
from cart
where ordernummer = '176748409'
group by userid
) x
要回答第二個問題,你可以只做一個簡單的JOIN
,如:
select
sum(x.m)
from (
select max(u.grundfrakt) as m
from cart c
join users u on u.userId = c.userId
where c.ordernummer = '176748409'
group by u.userid
) x
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.