根据 mysql 查询中的外键值获取特定类别的 2 条记录?
Get 2 record of a particular category based on foreignkey value in mysql query?
我有两个 table product
和 mastercategory
产品有列
id - pk,
title- varchar,
price - varchar,
selling_price - varchar,
description - varchar,
is_approved - bool,
is_live - bool,
is_visible - bool,
category_id - fk (foreign_key of category table)
mastercategory 有列
id - pk,
name - varchar,
is_active - bool,
is_hotcategory - bool,
我想获取每个类别的 2 条最新记录,其中 is_hotcategory
是 true
我怎样才能得到这个
我试过这个:
select cp.id,cp.title, cp.category_id from product cp
left join mastercategory cmc on cp.category_id = cmc.id where cmc.is_hotcategory = 1 and cp.id in (
select max(cp.id) As id
from product cp
where cp.is_visible = 1 and cp.is_live= 1 and cp.is_approved=1
group by category_id
union all
select max(cp.id) As id
from product cp
left join mastercategory cmc on cp.category_id = cmc.id
where cmc.is_hotcategory = 1 and
cp.is_visible = 1 and cp.is_live= 1 and cp.is_approved=1 and
(cp.id not in (select max(cp.id)
from product cp
where cp.is_visible = 1 and cp.is_live= 1 and cp.is_approved=1 group by category_id )) group by category_id
) order by category_id asc;
我正在获取每个类别的最后一条记录,然后获取每个类别的第二条最后记录,然后结合两个搜索查询
我认为它会起作用,但如果我必须获得每个类别的 2 条以上记录怎么办。
还有其他解决办法吗?
WITH cte AS (
select cp.id, cp.title, cp.category_id,
ROW_NUMBER() OVER (PARTITION BY cp.category_id ORDER BY cp.id DESC) rn
from product cp
join mastercategory cmc on cp.category_id = cmc.id
where cmc.is_hotcategory
and cp.is_visible
and cp.is_live
and cp.is_approved
)
SELECT *
FROM cte
WHERE rn < 3;
我假设“每个类别的最新记录”是具有相同category_id
的行中具有最大id
的产品行(我没有找到类似于[=13=的列]).如果不是,则相应地调整框架规格。
我有两个 table product
和 mastercategory
产品有列
id - pk,
title- varchar,
price - varchar,
selling_price - varchar,
description - varchar,
is_approved - bool,
is_live - bool,
is_visible - bool,
category_id - fk (foreign_key of category table)
mastercategory 有列
id - pk,
name - varchar,
is_active - bool,
is_hotcategory - bool,
我想获取每个类别的 2 条最新记录,其中 is_hotcategory
是 true
我怎样才能得到这个
我试过这个:
select cp.id,cp.title, cp.category_id from product cp
left join mastercategory cmc on cp.category_id = cmc.id where cmc.is_hotcategory = 1 and cp.id in (
select max(cp.id) As id
from product cp
where cp.is_visible = 1 and cp.is_live= 1 and cp.is_approved=1
group by category_id
union all
select max(cp.id) As id
from product cp
left join mastercategory cmc on cp.category_id = cmc.id
where cmc.is_hotcategory = 1 and
cp.is_visible = 1 and cp.is_live= 1 and cp.is_approved=1 and
(cp.id not in (select max(cp.id)
from product cp
where cp.is_visible = 1 and cp.is_live= 1 and cp.is_approved=1 group by category_id )) group by category_id
) order by category_id asc;
我正在获取每个类别的最后一条记录,然后获取每个类别的第二条最后记录,然后结合两个搜索查询
我认为它会起作用,但如果我必须获得每个类别的 2 条以上记录怎么办。 还有其他解决办法吗?
WITH cte AS (
select cp.id, cp.title, cp.category_id,
ROW_NUMBER() OVER (PARTITION BY cp.category_id ORDER BY cp.id DESC) rn
from product cp
join mastercategory cmc on cp.category_id = cmc.id
where cmc.is_hotcategory
and cp.is_visible
and cp.is_live
and cp.is_approved
)
SELECT *
FROM cte
WHERE rn < 3;
我假设“每个类别的最新记录”是具有相同category_id
的行中具有最大id
的产品行(我没有找到类似于[=13=的列]).如果不是,则相应地调整框架规格。