Mysql
 sql >> Database >  >> RDS >> Mysql

SQL:sottoquery per il risultato più alto senza ordine per

Devi avere un'altra sottoquery per questo,

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
                )
            )