简体   繁体   English

MySQL-获取具有相同值的行

[英]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? 假设我的ID为1 ,如何在会话A和C中获得与我交谈的ID为24的用户?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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