计算百分比,但我得到语法错误

时间:2015-09-02 00:06:57

标签: mysql

请在尝试时查看此代码,我会收到语法错误。

SELECT Category,  
    COUNT(*) AS Total, 
    (COUNT(*) / (SELECT COUNT(*) FROM Item WHERE Department='Popular')) * 100
    AS 'Percentage to all items', 
FROM Item
WHERE Department='Popular'
GROUP BY Category;

1 个答案:

答案 0 :(得分:2)

'Percentage to all items'**,**后有一个额外的逗号,是错字还是有意?应删除此逗号,然后尝试plz:)

所以sql应该是:

SELECT Category, COUNT(*) AS Total , 
    (COUNT(*) / (SELECT COUNT(*) FROM Item WHERE Department='Popular')) * 100 AS 'Percentage to all items'
    FROM Item WHERE Department='Popular' GROUP BY Category;