简体   繁体   中英

MySQL - Get rows with same value

I have two tables for an app with a conversation feature. It has a sessions table which holds conversation sessions and a session participants table that holds the participants for a conversation. Now, I want to get all the other participants that I have a conversation with.

`sessions` table
+----+---------+
| id | session |
+----+---------+
|  1 | A       |
|  2 | B       |
|  3 | C       |
+----+---------+


`session_participants` table
+----+---------+------+
| id | session | user |
+----+---------+------+
|  1 | A       |    1 |
|  2 | A       |    2 |
|  3 | B       |    3 |
|  4 | B       |    2 |
|  5 | C       |    1 |
|  6 | C       |    4 |
+----+---------+------+

Let say my id is 1 , how can I get users with the id 2 & 4 which I have a conversation with in the sessions A & C?

Thank you for your responses.

SELECT sp2.user
FROM session_participants AS sp1
JOIN session_participants AS sp2 ON sp2.session = sp1.session AND sp2.id != sp1.id
WHERE sp1.user = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM