SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create table number (id serial, country varchar); create table service (id serial, name varchar); create table buy_number (id serial, numberId int, serviceId int); insert into number values (1, 'Russia'), (2, 'Germany'), (3, 'Argentina'); insert into service values (1, 'Service1'), (2, 'Service3'), (3, 'Service3'); insert into buy_number values (1, 1, 1), (2, 2, 1), (3, 2, 2), (4, 2, 3); SELECT pn.country, array_agg(s.name) services FROM number pn CROSS JOIN service s LEFT JOIN buy_number bn ON bn.numberId = pn.id AND bn.serviceId = s.id WHERE bn.id is null group by pn.country
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear