[英]MySQL - selecting the count of an id where one count is greater than another count
這里的sql語句很新,所以這對我來說有點棘手。
我有兩個表:已售出和產品。 已售出(SID,UPC,日期)產品(UPC,名稱,品牌)
我需要找出一個品牌的銷量超過另一個品牌的店鋪數(sid)。
我以為是這樣的:
select count(*) from sold natural join product
where count(brand = 'sony') > count(brand = 'samsung');
顯然這是無效的...
SELECT COUNT(*)
FROM (
SELECT SID
FROM Sold
JOIN product
ON product.UPC = Sold.UPC -- otherwise we have a cartesian product
GROUP BY SID -- if we need totals _by store_, we need to group on that.
HAVING SUM(brand='sony') > SUM(brand='samsung')
) Totals.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.