我已经这么做了很长时间了:
SELECT COUNT(*) AS 'Rank' FROM Table
WHERE Condition = 'Condition' AND Score >= 'Score';
SELECT COUNT(*) AS 'Total' FROM Table
WHERE Condition = 'Condition';
是否有更有效的方法来获得Rank和Total?
答案 0 :(得分:3)
您只需一次通过数据即可同时计算两者。
SELECT COUNT(*) AS 'Total',
SUM(CASE WHEN Score >= 'Score' THEN 1 ELSE 0 END) AS `Rank`
FROM Table
WHERE Condition = 'Condition';