With the following SQL query which I want to add an array of JSON objects which has data from another table, based on the posts.liked_by array:
SELECT
p.id,
p.author,
p.content,
u2.id,
u2.username,
p.liked_by AS likedby
FROM posts p
INNER JOIN users u1
ON p.author = u1.id
LEFT JOIN users u2
ON u2.id = ANY(p.liked_by)
I'm getting the expected output of
╔════╤════════╤═════════════╤══════════╤═════════╗
║ id │ author │ content │ username │ likedby ║
╠════╪════════╪═════════════╪══════════╪═════════╣
║ 1 │ 1 │ Lorem Ipsum │ John Doe │ {1, 2} ║
╚════╧════════╧═════════════╧══════════╧═════════╝
Now, I'd like to modify the likedby column to be an array of objects with user data, accoring to something along the lines of this:
+----+--------+-------------+----------+-----------------------------------------------------------------+
| id | author | content | username | liked_by |
+----+--------+-------------+----------+-----------------------------------------------------------------+
| 1 | 1 | Lorem Ipsum | John Doe | [{id: 1, username: "John Doe"}, {id: 2, username: "Sam Smith"}] |
+----+--------+-------------+----------+-----------------------------------------------------------------+
with data of the posts table being structured like
+----+--------+-------------+-----------+-----------+
| id | author | content | author_id | liked_by |
+----+--------+-------------+-----------+-----------+
| 1 | 1 | lorem ipsum | 1 | {1, 2, 3} |
+----+--------+-------------+-----------+-----------+
and the user table being structured as
+----+----------+
| id | username |
+----+----------+
| 1 | John Doe |
+----+----------+
How would I go about doing this?
FROM public.posts AS posts, public.usersremove:, public.usersuserstable. For reference, I changed it to FROM public.posts AS posts LEFT JOIN users u ON users.id = ANY (posts.likedby)