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

MySQL Limit LEFT JOIN Sottoquery dopo l'adesione

Questo ti darà 5 commenti per ogni post.

SELECT  p.*,
        c.*
FROM    Post p
        LEFT JOIN
        (
            SELECT  a.*
            FROM    Comments a
            WHERE    
                    (
                       SELECT   COUNT(*) 
                       FROM     Comments b
                       WHERE    a.Post_ID = b.Post_ID AND 
                                a.ID <= b.ID
                    ) <= 5
        ) c ON  a.ID = c.Post_ID