I have working with Relational Databases for a while but I'm pretty new with PostgreSQL and handling Jsons in dbs. I have the following example:
CREATE TEMPORARY TABLE IF NOT EXISTS Tags
(
Id INT,
Description VARCHAR(100)
)
ON COMMIT DELETE ROWS;
CREATE TEMPORARY TABLE IF NOT EXISTS EntitiesWithTags
(
Id INT,
jsonArray JSON
)
ON COMMIT DELETE ROWS;
INSERT INTO Tags VALUES (1, 'Test');
INSERT INTO Tags VALUES (2, 'Hello');
INSERT INTO Tags VALUES (3, 'Goodbye');
INSERT INTO EntitiesWithTags VALUES(1, '[1, 2]');
INSERT INTO EntitiesWithTags VALUES(2, '[1, 3]');
INSERT INTO EntitiesWithTags VALUES(3, '[2]');
SELECT * FROM EntitiesWithTags;
The result of the select is this:
+----+-----------+
| id | jsonArray |
+----+-----------+
| 1 | [1, 2] |
+----+-----------+
| 2 | [1, 3] |
+----+-----------+
| 3 | [2] |
+----+-----------+
But I would like to achive this:
+----+---------------------+
| id | jsonArray |
+----+---------------------+
| 1 | ["Test", "Hello"] |
+----+---------------------+
| 2 | ["Test", "Goodbye"] |
+----+---------------------+
| 3 | ["Hello"] |
+----+---------------------+
In summary, I need to join the ids stored in a json array with its corresponding records in a regular table. I need to select the descriptions and not the ids in the array.
Thank you in advance.