Answer the question
In order to leave comments, you need to log in
How to combine two queries into one Postgresql?
The task is to select a list of people from the "people" table according to a certain condition, but at the same time, check if the person from the selected list is added as a friend in another table "user_friends".
It can be done with two queries:
1. Get a list of users from the "people" table
SELECT * FROM people
WHERE people.name LIKE $(regex)
LIMIT $(limit);
SELECT *
FROM user_friends
WHERE user_friends.user_id = $(user_id) AND user_friends.friend_id IN ($1:csv)
Answer the question
In order to leave comments, you need to log in
WITH peoples AS (
SELECT * FROM people
WHERE people.name LIKE $(regex)
LIMIT $(limit)
)
SELECT f.*
FROM user_friends f
JOIN peoples p.id = f.friend_id
WHERE user_friends.user_id = $(user_id)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question