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

SQLize | PHPize | SQLtest

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

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
create table categories ( id int, name varchar(64) ); insert into categories values (1, 'Category1'),(2, 'Category2'); create table products ( id int, cat_id int, name varchar(64), price int ); insert into products values (1, 1, 'Prod1', 10),(2, 1, 'Prod2', 12), (3, 2, 'Prod3', 10); SELECT cat.id, cat.name, JSON_ARRAYAGG( JSON_OBJECT('pro_name',pro.name,'pro_pice',pro.price) ) prod FROM products as pro JOIN categories as cat on pro.cat_id = cat.id GROUP BY cat.id, cat.name ORDER BY cat.id;

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

Copy Clear