I have 2 table
CREATE TABLE json_data_table(d_id int, d1_json json, d2_json json);
CREATE TABLE user_data(uid int, username varchar);
and data respectively are
INSERT INTO user_data VALUES
(1,'test_user_1'),
(2,'test_user_2'),
(3,'test_user_3');
INSERT INTO json_data_table VALUES
(1,'{"stage1":1,"stage2":2 }', '{
"stage1": {
"date": "12-01-2023",
"status": "open",
"uid": "2"
},
"stage2": {
"date": "22-01-2023",
"status": "close",
"uid": "1"
}
}'),
(2,'{"stage1":11,"stage2":21 }', '{
"stage1": {
"date": "21-2-2023",
"status": "open",
"uid": "3"
},
"stage2": {
"date": "2-2-2023",
"status": "close",
"uid": "2"
}
}');
Now i want to join user_data
table with json_data_table
with uid
to get username
Expected table output:
I got the other columns by simply fetching json->>key_name
here is select
query.
SELECT d1_json->>'stage1' as stage1,
d1_json->>'stage2' as stage2,
d2_json->'stage1'->>'date' as s1_date,
d2_json->'stage1'->>'status' as s1_status,
d2_json->'stage1'->>'uid' as s1_uid,
d2_json->'stage2'->>'date' as s2_date,
d2_json->'stage2'->>'status' as s2_status,
d2_json->'stage2'->>'uid' as s2_uid
FROM json_data_table AS jd;
How to join table to get expected table as shown in image?
2
Answers
You just join it and use the expression to extract the
uid
in the join condition.And since you need to different sets of
user_data
you need to join the table twice.And finally since
uid
is a number and JSON consists of text you have to convert between the two.I opted to do the conversion on the JSON side, since this enables usage of an index for
user_data
which seems reasonable to exist in the real use case.DB-Fiddle to play with.
Try this: