Limit a Left Join on the first table

前端 未结 2 976
陌清茗
陌清茗 2021-02-14 06:51

I have two tables: gems and gemdetail that are left joined. I am trying to limit the LEFT JOIN to 10 records in the gems table. There are 2 other tables joined (gemreply and use

2条回答
  •  春和景丽
    2021-02-14 07:40

    Try this:

    SELECT g.gemid, g.title, r.tot, gemdetail.filename
    FROM (SELECT * FROM gems WHERE grade = '7' LIMIT 10) g
    LEFT JOIN (SELECT gemid, COUNT(*) AS tot FROM gemreply GROUP BY gemid) r
              ON r.gemid = g.gemid
    LEFT JOIN gemdetail ON g.gemid = gemdetail.gemid
    LEFT JOIN users ON g.userid = users.userid
    ORDER BY g.gemid;
    

    This should work.

提交回复
热议问题