SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
-- Hint: use Ctrl+Enter for SQL autocomplete CREATE TABLE clients ( t_id serial, t_name varchar(64), t_phone varchar(64) ); INSERT INTO clients VALUES (10, 'Client10', 'Client10Phone'); CREATE TABLE trainers ( t_id serial, t_name varchar(64), t_phone varchar(64) ); INSERT INTO trainers VALUES (1, 'Client1', 'Client1Phone'), (2, 'Client2', 'Client2Phone'), (3, 'Client3', 'Client3Phone'); SELECT row_to_json(client.*) FROM ( SELECT clients.*, json_agg( row_to_json(trainers.*) ) trainers FROM clients JOIN trainers ON trainers.t_id < clients.t_id where clients.t_id = 10 GROUP BY clients.t_id, clients.t_name, clients.t_phone ) client;
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear