Skip to content
Advertisement

Get 2 record of a particular category based on foreignkey value in mysql query?

I have two table product and mastercategory

product has columns

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 has columns

id - pk, 
name - varchar, 
is_active - bool,
is_hotcategory - bool,

I want to get 2 latest record of each category where is_hotcategory is true

How can I get this

I tried it with this:

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;

I’m getting last record of each category then getting 2nd last record of each category then with union combining both the search query

I think it will work but what if I have to get more than 2 record of each category. Is there any other solution to that.

Advertisement

Answer

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;

I assume that “latest record of each category” is the product row with greatest id from the rows with the same category_id (I have not found a column similar to created_at). If not then adjust frame specification accordingly.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement