如何使用

时间:2016-08-13 06:45:28

标签: php arrays message

enter image description here

我原以为这很容易,但坚持使用发送方接收器阵列找到对话列表的问题,上面是数据库的快照

我已按照上述顺序使用发件人ID和收件人ID进行分组,但仍坚持获取唯一的对话

这是查询

SELECT tbl_registration.name as sender_name,group_concat(message SEPARATOR '|') as message_list,`sender_reciever_array`,`datetime`,`reciever_id`,`sender_id` FROM `tbl_chatting_view` 
inner join 
tbl_registration
on tbl_registration.id=tbl_chatting_view.sender_id
group by `sender_reciever_array`

在php数组中产生以下结果

array(5) {
  [0]=>
  array(6) {
    ["sender_name"]=>
    string(7) "harshit"
    ["message_list"]=>
    string(4) "k|my"
    ["sender_reciever_array"]=>
    string(4) "2,29"
    ["datetime"]=>
    string(19) "2016-08-06 13:34:09"
    ["reciever_id"]=>
    string(2) "29"
    ["sender_id"]=>
    string(1) "2"
  }
  [1]=>
  array(6) {
    ["sender_name"]=>
    string(7) "harshit"
    ["message_list"]=>
    string(5) "mohan"
    ["sender_reciever_array"]=>
    string(3) "2,5"
    ["datetime"]=>
    string(19) "2016-08-08 12:18:45"
    ["reciever_id"]=>
    string(1) "5"
    ["sender_id"]=>
    string(1) "2"
  }
  [2]=>
  array(6) {
    ["sender_name"]=>
    string(5) "admin"
    ["message_list"]=>
    string(9) "jkgh|test"
    ["sender_reciever_array"]=>
    string(4) "29,5"
    ["datetime"]=>
    string(19) "2016-08-02 11:31:45"
    ["reciever_id"]=>
    string(1) "5"
    ["sender_id"]=>
    string(2) "29"
  }
  [3]=>
  array(6) {
    ["sender_name"]=>
    string(5) "admin"
    ["message_list"]=>
    string(56) "Jhadjksdhjkahsjkhdfjshfjhdsjfh|Asdasfdfs|Hgasdhjgdhgsahj"
    ["sender_reciever_array"]=>
    string(5) "29,63"
    ["datetime"]=>
    string(19) "2016-08-12 12:47:53"
    ["reciever_id"]=>
    string(2) "63"
    ["sender_id"]=>
    string(2) "29"
  }
  [4]=>
  array(6) {
    ["sender_name"]=>
    string(10) "Kiran sahu"
    ["message_list"]=>
    string(156) "Asdafdggfhgfj|HhjjhhjjhhFghxfzcgc

Bnasbdjnbfjdjfh


Ninja admonishments

Njkdnsfjkndgnkdfn

|Ghjgsdfhjgfh|Sadas|Dgfjdjkjkhsjkjkas
Dsjmjkdsljflkjsdlkfcjklsd"
    ["sender_reciever_array"]=>
    string(5) "63,29"
    ["datetime"]=>
    string(19) "2016-08-13 05:39:56"
    ["reciever_id"]=>
    string(2) "29"
    ["sender_id"]=>
    string(2) "63"
  }
}

在上面的数组中,最后一条消息将出现在消息数组中,其中列表的分隔符为“|”,并且发件人和接收者数组中包含two array 63,29 and 29,63但我需要两条消息的组合

1 个答案:

答案 0 :(得分:0)

你可以搞砸这个。规范化GROUP BY以包含对话的两面?我不是SQL专家,所以不要忘了。 (接收器拼写错误):D

SELECT IF(tcv.sender_id > tcv.reciever_id,
           concat_ws(',', tcv.reciever_id, tcv.sender_id),
           concat_ws(',', tcv.sender_id, .tcvreciever_id)
       ) as both
FROM tbl_chatting_view tcv
GROUP BY both