我有一个SQL查询,它正在获取如下结果:
[{"id":89,"sender_id":2,"reciever_id":1,"message":"no reply","deleted_at":null,"created_at":"2017-04-04 17:01:20","updated_at":"2017-04-04 17:01:20"},
{"id":88,"sender_id":1,"reciever_id":2,"message":"hiiiii","deleted_at":null,"created_at":"2017-04-04 16:59:47","updated_at":"2017-04-04 16:59:47"},
{"id":87,"sender_id":5,"reciever_id":1,"message":"hiiiii","deleted_at":null,"created_at":"2017-04-04 16:59:43","updated_at":"2017-04-04 16:59:43"},
{"id":86,"sender_id":1,"reciever_id":5,"message":"hiiii","deleted_at":null,"created_at":"2017-04-04 16:59:38","updated_at":"2017-04-04 16:59:38"},
{"id":85,"sender_id":1,"reciever_id":5,"message":"hiii vRUN\n","deleted_at":null,"created_at":"2017-04-04 16:10:51","updated_at":"2017-04-04 16:10:51"}]
但我只想要sender_id
和reciever_id
的唯一组合。我只需要5行中的2行,因为只有2行具有唯一的组合。像这样:
[{"id":89,"sender_id":2,"reciever_id":1,"message":"no reply","deleted_at":null,"created_at":"2017-04-04 17:01:20","updated_at":"2017-04-04 17:01:20"},
{"id":87,"sender_id":5,"reciever_id":1,"message":"hiiiii","deleted_at":null,"created_at":"2017-04-04 16:59:43","updated_at":"2017-04-04 16:59:43"},
我怎样才能做到这一点?
答案 0 :(得分:0)
像这样添加 GROUP BY
SELECT *
FROM yourTable
GROUP BY SUBSTRING_INDEX(SUBSTRING_INDEX( ,',',3),':',-1)
<强>样品强>
mysql> SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('[{"id":89,"sender_id":2,"reciever_id":1,"message":"no reply","deleted_at":null,"created_at":"2017-04-04 17:01:20","updated_at":"2017-04-04 17:01:20"},' ,',',3),':',-1) as result;
+--------+
| result |
+--------+
| 1 |
+--------+
1 row in set (0,00 sec)
mysql>