Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear
create table tbl ( id int primary key auto_increment, item_id int, likes int ); insert into tbl (item_id, likes) values (1, 200), (1, 300), (1, 400), (1, 4), (2, 54), (2, 31), (3, 22); with n as ( select id, item_id, likes, row_number() over (partition by item_id) rn from tbl ) select item_id, likes from n where rn < 4;

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php $query = "with n as ( select id, item_id, likes, row_number() over (partition by item_id) rn from tbl ) select item_id, likes from n where rn < 4;"; // Select using Laravel $rows = $db::select($db::raw($query)); var_export($rows);
Copy Clear