我有两个表如下:
消息
+----+--------+-----------------+
| id | sender | body |
+----+--------+-----------------+
| 11 | 4 | test msg one |
| 12 | 4 | test msg |
| 13 | 1 | this is test |
| 14 | 4 | WANT TO SHOW G1 |
| 15 | 4 | WANT TO SHOW G2 |
+----+--------+-----------------+
message_receivers
+----------+------------+
| receiver | message_id |
+----------+------------+
| 1 | 11 |
| 1 | 12 |
| 4 | 13 |
| 1 | 14 |
| 3 | 15 |
+----------+------------+
我写了以下查询
SELECT
`messages`.`id`,
`messages`.`body` ,
`messages`.`sender`,
MAX(`messages`.`id`) AS MID,
IF(`messages`.`sender`>`message_receivers`.`receiver`,`CONCAT_WS`(',',`messages`.`sender`,`message_receivers`.`receiver`),
`CONCAT_WS`(',',`message_receivers`.`receiver`,`messages`.`sender`)) AS `conc`
FROM
`messages`
JOIN `message_receivers` ON `messages`.`id` = `message_receivers`.`message_id`
WHERE
`message_receivers`.`receiver` = '4'
OR `messages`.`sender` = '4'
GROUP BY conc
这给了我以下结果
+----+-----------------+--------+------+------+
| id | body | sender | MID | conc |
+----+-----------------+--------+------+------+
| 11 | test msg one | 4 | 14 | 4,1 |
| 15 | WANT TO SHOW G2 | 4 | 15 | 4,3 |
+----+-----------------+--------+------+------+
但我想要
+----+-----------------+--------+------+------+
| id | body | sender | MID | conc |
+----+-----------------+--------+------+------+
| 14 | WANT TO SHOW G1 | 4 | 14 | 4,1 |
| 15 | WANT TO SHOW G2 | 4 | 15 | 4,3 |
+----+-----------------+--------+------+------+
我应该怎么办?提前致谢。