Skip to content
Advertisement

Get model with N child items grouped by field

Table looks like this:

JavaScript

How to get 3 or less items for each item_id with most likes?
So for my example output should looks like this:

JavaScript

Advertisement

Answer

Since MySQL 8.0 you can use window function row_number

JavaScript

Test Mysql window functions

JavaScript

Laravel DB query

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