X
X
xXRustamXx2020-08-09 23:05:14
PostgreSQL
xXRustamXx, 2020-08-09 23:05:14

Can this be done in postgresql?

Tables:
5f3055602c951191163070.png
Find 2 users by id who are in one personal dialog, how to do? Detailed: users id=1, id=2, they have the same dialogId, and the dialog has type='personal'.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xXRustamXx, 2020-08-10
@xXRustamXx

SELECT dialogs_users.* FROM dialogs_users
LEFT JOIN dialogs ON dialogs.id = dialogs_users."dialogId"
WHERE 
    dialogs.type = 'personal' 
    AND 
    dialogs_users."userId" IN ('69e56a68-edbd-4f8b-8ccd-cb8031c5c865', '06b475b7-8f12-4879-bea4-e8f22ef85a46') 
    AND
    dialogs_users."dialogId" IN (SELECT dialogs_users."dialogId" dialogId FROM dialogs_users GROUP BY dialogId HAVING COUNT(*) > 1);

V
Vasily Bannikov, 2020-08-09
@vabka

If I understand correctly, then

SELECT 
  du.userId 
FROM 
  dialogs_users du 
  INNER JOIN dialogs d 
    ON du.dialogId = d.id 
WHERE 
  d.type = 'personal' AND du.dialogId = $dialogId

Well, I advise you to re-read some book from the category "SQL for Dummies"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question