如何在LEFT JOIN上使用LIMIT?

时间:2018-12-29 12:51:11

标签: mysql orm eloquent mariadb relational-database

我有两个名为categoriesproducts的表,具有以下关系:

categories.category_id = products.product_category_id

我想用2个产品显示所有类别!

[
    [category 1] =>
            [
                'category_id' => 1,
                'category_title' => 'category_title 1',
                [products] => [
                    '0' => [
                            'product_category_id' => 1,
                            'product_id' => 51,
                            'product_title' => 'product_title 1',
                        ],
                    '1' => [
                            'product_category_id' => 1,
                            'product_id' => 55,
                            'product_title' => 'product_title 2',
                        ]
                ]
            ],
    [category 2] =>
            [
                'category_id' => 2,
                'category_title' => 'category_title 2',
                [products] => [
                    '0' => [
                            'product_category_id' => 2,
                            'product_id' => 32,
                            'product_title' => 'product_title 3',
                        ],
                    '1' => [
                            'product_category_id' => 2,
                            'product_id' => 33,
                            'product_title' => 'product_title 4',
                        ]
                ]
            ],
    ...
]

Laravel雄辩地说,我可以使用这样的东西:

$categories = Category::with(['products' => function($q) {
    $q->limit(2)
}])->get();

但是我不使用Laravel,我需要纯SQL代码! 我已经尝试过以下代码:

SELECT
    CT.category_id,
    PT.product_category_id,
    CT.category_title,
    PT.product_id,
    PT.product_title
FROM
    categories CT
LEFT JOIN(
SELECT
    *
FROM
    products 
    LIMIT 2
) AS PT
ON
    CT.category_id = PT.product_category_id
WHERE
    CT.category_lang = 'en' 

但是此代码有问题!看来,MYSQL首先在products表中获得2个第一行,然后尝试从LEFT JOIN到该2行中有一个categories!并导致返回产品的null值(而如果我删除LIMIT,效果很好,但我需要LIMIT

我也测试了以下代码:(更新

SELECT
    CT.category_id,
    PT.product_category_id,
    CT.category_title,
    PT.product_id,
    PT.product_title
FROM
    categories CT
LEFT JOIN(
    SELECT
        *
    FROM
        products
    WHERE
        product_category_id = CT.category_id
    LIMIT 5
) AS PT
ON
    CT.category_id = PT.product_category_id
WHERE
    CT.category_lang = 'en'

但是我收到此错误:

  

1054-“ where子句”中的未知列“ CT.category_id”

我无法访问子查询中的CT.category_id

什么是最佳解决方案?

1 个答案:

答案 0 :(得分:3)

在MySQL中,您将需要使用变量。基本思想是:

select p.*
from (select p.*,
             (@rn := if(@c = product_category_id, @rn + 1,
                        if(@c := product_category_id, 1, 1)
                       )
             ) as rn
      from (select p.* 
            from products p
            order by p.product_category_id
           ) p cross join
           (select @c := -1, @rn := 0) params
     ) p
where rn <= 2;

您可以将其用作子查询,然后加入其余的类别信息。

如果此代码有效,

Here是db <>小提琴。

在MySQL 8 + / MariaDB 10.2+中,这更容易编写为:

select p.*
from (select p.*,
             row_number() over (partition by product_category_id order by product_category_id) as seqnum
      from products p
     ) p
where seqnum <= 2;

注意:您可以通过调整两个查询中的order by子句来指定所需的2种产品(例如,前两个字母)。

编辑:

在MariaDB 10.2中,您显然不能使用子查询进行排序,因此适当的查询应为:

select p.*
from (select p.*,
             (@rn := if(@c = product_category_id, @rn + 1,
                        if(@c := product_category_id, 1, 1)
                       )
             ) as rn
      from products p cross join
           (select @c := -1, @rn := 0) params
      order by p.product_category_id
     ) p
where rn <= 2;

Here是db <>小提琴。

您也可以使用此方法:

select p.*
from products p
where (select count(*)
       from products p2
       where p2.product_category_id = p.product_category_id and p2.id <= p.id
      ) <= 2;

我真的不推荐这样做,因为相关子查询的性能通常比order by差得多。但是,如果每个类别的产品没有太多,那么就可以了(使用正确的索引)。