Answer the question
In order to leave comments, you need to log in
Select in JSON Object?
Hello everyone, help who knows ..
There is a PL\SQL function similar to this
FUNCTION myfunc(p_id varchar2) return in number
BEGIN
SELECT t_id, t_name, t_phone FROM clients where t_id = p_id;
SELECT t_id, t_name, t_phone FROM trainers where t_id < p_id;
RETURN;
END myfunc;
Answer the question
In order to leave comments, you need to log in
Below is a query variant (PostgreSQL) that collects everything into one JSON:
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;
SELECT json_object(
't_id' VALUE client.t_id,
't_name' VALUE client.t_name,
't_phone' VALUE client.t_phone,
't_trainers' VALUE client.trainers
) FROM (
SELECT
clients.*,
json_arrayagg(json_object(
't_id' VALUE trainers.t_id,
't_name' VALUE trainers.t_name,
't_phone' VALUE trainers.t_phone
)) 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;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question