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

mastercategory has columns

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:

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

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