0

次のようなmysqlテーブルチャットがあります

+---------+-----------+-------------+----------------+
| chat_id | sender_id | receiver_id | msg            |
+---------+-----------+-------------+----------------+
|       1 |      1002 |        1001 | test           |
|       2 |      1001 |        1002 | test           |
|       3 |      1002 |        1001 | test           |
|       5 |      1001 |        1002 | asdf           |
|       6 |      1003 |        1001 | tesdf          |
|       9 |      1001 |        1003 | tasdfa a fasd  |
|      10 |      1001 |        1004 | dsf asdf a     |
|      11 |      1005 |        1001 | dsf asdf asdf  |
+---------+-----------+-------------+----------------+

ユーザー 1002、1003、1004、1005 の間でユーザー 1001 の会話があります。

ユーザー 1001 と会話したユーザー (1002,1003,1004,1005) のリストが必要です。

mysql クエリはどうなりますか? 私を助けてください。

4

5 に答える 5

3

これを試してみてください

select  DISTINCT if(receiver_id='1001',sender_id,receiver_id) AS id
from    YourTable
where   (sender_id = 1001 OR receiver_id = 1001)

編集:

SELECT GROUP_CONCAT( DISTINCT (
if( receiver_id = '1001', sender_id, receiver_id ) )
) AS concat
FROM chat
WHERE (
sender_id =1001
OR receiver_id =1001
)

GROUP_CONCAT のサイズの問題を認識していると思います。これを参照できますhttp://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

于 2013-09-12T09:22:15.180 に答える
0
(select distinct receiver_id from table where sender_id = 1001)
union distinct
(select distinct sender_id from table where receiver_id = 1001);
于 2013-09-12T11:44:08.073 に答える