![](/img/trans.png)
[英]Mysql display one colum if sender id and receiver id values mathes in any of the colums
[英]MYSQL - Query for getting distinct sender_id and receiver id and richiesta_id
我有這個表“消息”結果:
我試過:
SELECT idutente, richiesta_id
FROM (
SELECT sender_id, richiesta_id
FROM messages
UNION
SELECT receiver_id, richiesta_id
FROM messages
) AS DistinctCodes (idutente)
WHERE idutente IS NOT NULL;
但不工作或不正確。 我怎樣才能從表中得到你看到的表:
| richiesta_id | users |
|---------------------|-------------------------------------|
| 55 | 2, 3, 4 |
| other richiesta_id | other users list separated by comma |
我認為您需要結合使用“GROUP BY”和“GROUP_CONCAT”來歸檔它。
SELECT DistinctCodes.richiesta_id, GROUP_CONCAT(DISTINCT DistinctCodes.uid ORDER BY DistinctCodes.uid ASC)
FROM (
SELECT sender_id AS uid, richiesta_id
FROM messages
UNION
SELECT receiver_id AS uid, richiesta_id
FROM messages
) AS DistinctCodes
GROUP BY DistinctCodes.richiesta_id
簡化它:
SELECT sender_id as idutente, richiesta_id
FROM messages
WHERE sender_id IS NOT NULL
UNION
SELECT receiver_id, richiesta_id
FROM messages
WHERE receiver_id IS NOT NULL
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.