Limit a Left Join on the first table

前端 未结 2 959
陌清茗
陌清茗 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.

    0 讨论(0)
  • 2021-02-14 07:41

    Something like that

       SELECT * FROM A
          INNER JOIN ( SELECT * FROM A WHERE A.FIELD1='X' ORDER BY A.FIELD2 LIMIT 10) X
                 ON (A.KEYFIELD=X.KEYFIELD)
          LEFT JOIN B ON (A.FIELD = B.FIELD)
          LEFT JOIN C ON (A.FIELD = C.FIELD)
    
    0 讨论(0)
提交回复
热议问题