0

假设我有 2 张桌子,我想计算最畅销的艺术家:

  • 音乐(id、标题、条形码)
  • 艺术家(条形码,艺术家)

    SELECT COUNT(m.barcode) AS sells, m.barcode, artist
    FROM music m
    LEFT JOIN artists a
    ON m.barcode = a.barcode
    GROUP BY m.barcode ORDER BY sells DESC
    

此查询返回例如:

  sells    | barcode |   artist
 ---------------------------------
   1000        123       jdoe
    500        223       kloe
    100        321       jdoe

我如何计算 jdoe 复杂地销售了 1100 首音乐?

提前感谢您的任何提示

4

2 回答 2

1

您应该将group byfrom更改barcodeartist(并barcodeselect语句中删除):

SELECT COUNT(m.barcode) AS sells, artist
FROM music m LEFT JOIN
     artists a
     ON m.barcode = a.barcode
GROUP BY artist
ORDER BY sells DESC;
于 2013-07-30T01:10:00.900 回答
0
SELECT COUNT(m.barcode) AS popular, m.barcode, a.artist, SUM(a.sell) 
FROM `music` AS m 
LEFT JOIN `artists` AS a 
ON (m.barcode = a.barcode)
WHERE a.artist = 'jdoe'
GROUP BY m.barcode 
ORDER BY `popular` DESC
于 2013-07-29T21:33:31.110 回答