SELECT N.National_PosID, N.Title, C.First_Name, C.Mid_Name, C.Last_Name, count(*)
FROM National_Position N, Candidate C, Vote V
WHERE N.National_PosID = C.National_PosID AND V.Candidate_ID = C.Candidate_ID
Group by N.National_PosID, N.Title, C.First_Name, C.Mid_Name, C.Last_Name;
输出
+------------+------------+----------+-----------+----------+
| Title | First_Name | Mid_Name | Last_Name | count(*) |
+------------+------------+----------+-----------+----------+
| Agueda | Sarahann | | MCMAHON | 557 |
| Amesha | Maurice | | GREEN | 1071 |
| Kiante | Jesicca | | WINTERS | 482 |
| Kieffer | Serene | | HIGGINS | 1285 |
| Koray | Ahley | | COLLIER | 1287 |
| SENATE | Daisy | Hao | MADDOX | 1316 |
| SENATE | Radhames | Jakson | MURILLO | 1269 |
| SENATE | Rayan | Kaitlynn | FRYE | 1089 |
| SENATE | Rio | Krysti | HUGHES | 457 |
| SENATE | Shalondra | Nila | HORTON | 551 |
+------------+------------+----------+-----------+----------+
10 rows in set (1.33 sec)
问题是如何获得最大计数的输出?
您可以使用subquery
获得原始结果的max
count
。
SELECT t1.National_PosID, t1.Title, t1.First_Name, t1.Mid_Name, t1.Last_Name, max(ct)
FROM(
SELECT N.National_PosID, N.Title, C.First_Name, C.Mid_Name, C.Last_Name, count(*) ct
FROM National_Position N
INNER JOIN Candidate C ON N.National_PosID = C.National_PosID
INNER JOIN Vote V ON V.Candidate_ID = C.Candidate_ID
INNER JOIN
GROUP BY N.National_PosID, N.Title, C.First_Name, C.Mid_Name, C.Last_Name) as t1
GROUP BY t1.National_PosID, t1.Title, t1.First_Name, t1.Mid_Name, t1.Last_Name