试图写一个COUNT(*)查询

时间:2012-12-19 06:57:58

标签: mysql

写一个查询,返回从ATunes购买了最多歌曲的前五位客户的名字,按购买次数(降序)排序。我还必须将结果限制在前5位。

SELECT P.CustomerID, A.FirstName, A.LastName, P.DateOfPurchase, COUNT(DateOfPurchase) as NumberOfPurchases
ORDER BY NumberOfPurchases DESC, LIMIT 0,5
FROM Purchases as P
JOIN ATunesCostumers as A on (A.CustomerID = P.CustomerID)
GROUP BY CustomerID;

这是我正在尝试的,我收到语法错误。 当我摆脱ORDER BY和LIMIT语句时,我得到了我需要的一切,除了限制和正确的排序。我很茫然,有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT P.CustomerID, A.FirstName, A.LastName, P.DateOfPurchase, COUNT(DateOfPurchase) as NumberOfPurchases
FROM Purchases as P
JOIN ATunesCostumers as A on P.CustomerID = A.CustomerID
GROUP BY CustomerID
ORDER BY NumberOfPurchases DESC LIMIT 0,5;