我有两个问题:
(一)
SELECT COUNT(*) AS total, COUNT(supporter) AS done FROM Supports;
2个)
SELECT supporter, COUNT(supporter) AS amount FROM Supports
GROUP BY supporter ORDER BY amount DESC LIMIT 1;
如何有效地组合它们?
这是桌子的样子:
+-----------------------------+
| id | name | supporter |
+-----------------------------+
| 1 | user1 | sup1 |
| 2 | user1 | sup2 |
| 3 | user1 | NULL |
| 4 | user2 | sup1 |
| 5 | user2 | sup3 |
+-----------------------------+
最佳答案
因为您需要总数,所以必须使用子查询合并为一个查询。将其包含在FROM子句中。
SELECT supporter, COUNT(supporter) AS amount, total, done
FROM Supports,
(SELECT COUNT(*) AS total, COUNT(supporter) AS done FROM Supports) totals
GROUP BY supporter
ORDER BY amount DESC
LIMIT 1;