SQL limit SELECT but not JOIN

后端 未结 2 881
北荒
北荒 2021-01-29 08:48

I\'m implementing pagination on my BD. My problem is when I want limit the SELECT statement but not the JOIN. Example, a product can got many prices:

SELECT * FR         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-29 09:08

    Modify your query to limit the number of product rows before joining it to the price table. This means we want to to join the results of a query to a table, or in other words, we write a query containing a subquery:

    SELECT *
    FROM (
        SELECT *
        FROM product
        ORDER BY id_product
        LIMIT 3
    ) p
    LEFT JOIN price ON p.id = price.id_product
    

    Hope that helps.

提交回复
热议问题