Mysql
 sql >> Datenbank >  >> RDS >> Mysql

SQL - Unterabfragen für Top-Ergebnisse ohne Sortierung nach

Dafür benötigen Sie eine weitere Unterabfrage,

SELECT c.Givenname, c.Familyname, COUNT(r.places) AS TotalPlaces
FROM Competitors c 
INNER JOIN Results r ON r.Competitornum = c.Competitornum
WHERE r.place IN (1,2,3)
GROUP BY c.Givenname, c.Familyname
HAVING COUNT(r.places) = 
            (
                SELECT MAX(TotalPlaces)
                FROM
                (
                    SELECT COUNT(g.places) AS TotalPlaces
                    FROM Competitors f 
                    INNER JOIN Results g ON f.Competitornum = g.Competitornum
                    WHERE g.place IN (1,2,3)
                    GROUP BY f.Givenname, f.Familyname
                )
            )