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

Seleziona i giocatori con numero di trofei e punti fissi

Per risultati più sicuri, avvolgi tutta la tua query corrente in una sottoquery.

SELECT p.id, p.name, p.surname, 
        IFNULL(trop.trophy_count, 0),
        IFNULL(pts.points, 0)
FROM dbo.Players p
    LEFT JOIN 
    (
        select p.id, count(t.player_id) as trophy_count
        from dbo.Players p 
                left join Trophies t  on t.player_id=p.id
        group by p.id
    ) trop ON p.id = trop.id
    LEFT JOIN
    (
        select p.id,sum(points) as points
        from dbo.Players p
            inner join dbo.Stats s on s.player_id = p.id    
        group by p.id
    ) pts ON p.id = pts.id