我有一個MySQL數據庫,並且我想將一個人的最低3點累加起來。MySQL獲得用戶的最低3個值
+--------+--------+
| Person | Points |
+--------+--------+
| 1 | 15 |
| 1 | 10 |
| 1 | 5 |
| 1 | 10 |
| 2 | 5 |
| 2 | 4 |
| 2 | 3 |
| 2 | 2 |
| 3 | 1 |
| 3 | 1 |
| 3 | 1 |
+--------+--------+
我想要的結果:
+-------+-----+
| 1 | 25 |
| 2 | 9 |
| 3 | 3 |
+-------+-----+
但我真的失去了如何解決這個問題。這是我查詢到現在爲止,:
SELECT person, SUM(points) FROM (SELECT SUM(points) FROM table
GROUP BY person ORDER BY points ASC LIMIT 3)
這是我的SQL創建腳本:
CREATE TABLE `mytable` (
`person` int(11) DEFAULT NULL,
`points` int(11) DEFAULT NULL
) ;
INSERT INTO `mytable` (`person`, `points`) VALUES
(1, 15),
(1, 10),
(1, 5),
(1, 10),
(2, 5),
(2, 4),
(2, 3),
(2, 2),
(3, 1),
(3, 1),
(3, 1);
見[_Groupwise MAX_](http://mysql.rjweb.org/doc.php/groupwise_max)。 –