现在我成功获得了此查询的最后一条消息
enter code here
$coreQueryUser = $this->Message->query('
select m.*
from
messages m
inner join (
select max(id) as maxid
from messages
where messages.list_id = 3
group By (if(sender_id > reciever_id, sender_id, reciever_id)),
(if(sender_id > reciever_id, reciever_id, sender_id))
) t1 on m.id=t1.maxid
');
当我运行此查询时,它给出了以下输出: -
enter code here
<pre>Array
(
[0] => Array
(
[m] => Array
(
[id] => 2
[sender_id] => 10
[reciever_id] => 21
[list_id] => 3
[message] => hello sir
[add_date] => 2016-09-25 08:24:38
[is_check] => 0
)
)
[1] => Array
(
[m] => Array
(
[id] => 7
[sender_id] => 10
[reciever_id] => 22
[list_id] => 3
[message] => hmm
[add_date] => 2016-09-27 00:00:00
[is_check] => 0
)
)
)
现在您在输出中看到第一个数组sender_id = 10和reciver_id = 21 和list_id = 3 列表由id为10的用户创建。在输出中我想获取用户ID为21的用户数据。请记住,当接收者将消息发送给发件人时,ID正在互换。在这种情况下,我还想要用户ID为21的数据
请帮帮我:(
答案 0 :(得分:0)
我找到了自己的解决方案
enter code here
$coreQueryUser = $this->Message->query('
select m.* ,u.*
from
messages m
inner join (
select max(id) as maxid
from messages
where messages.list_id = " '. $data['list_id'] .'"
group By (if(sender_id > reciever_id, sender_id, reciever_id)),
(if(sender_id > reciever_id, reciever_id, sender_id))
) t1 on m.id=t1.maxid
join
users u ON u.id = (CASE WHEN m.sender_id = 10
THEN m.reciever_id
ELSE m.sender_id
END)
');