从总数中获得排名

时间:2011-10-11 10:47:29

标签: mysql

我已经这么做了很长时间了:

SELECT COUNT(*) AS 'Rank' FROM Table
 WHERE Condition = 'Condition' AND Score >= 'Score';

SELECT COUNT(*) AS 'Total' FROM Table
 WHERE Condition = 'Condition';

是否有更有效的方法来获得Rank和Total?

1 个答案:

答案 0 :(得分:3)

您只需一次通过数据即可同时计算两者。

SELECT COUNT(*) AS 'Total', 
       SUM(CASE WHEN Score >= 'Score' THEN 1 ELSE 0 END) AS `Rank`
FROM Table
WHERE Condition = 'Condition';