我需要加入用户信息表和聊天消息查询以获得最后的回复。
聊天表
mid | sender | receiver| text | created
1 | chrys | Paul| Hello | 2015-12-08 20:00
2 | chrys2 | Chrys | Hey | 2015-12-08 20:10
对于我使用该脚本的最后一个回复结果:
mysql_query("
select *
from
chat
join
(select user, max(created) m , COUNT(*) AS msgCount
from
(
(select mid, receiver user, created
from chat
where sender='$login_session' )
union
(select mid, sender user, created
from chat
where receiver='$login_session')
) t1
group by user) t2
on ((sender='$login_session' and receiver=user) or
(sender=user and receiver='$login_session')) and
(created = m)
order by created desc
");
我的问题是我无法弄清楚如何加入用户信息表,如下所示:
id | username | photo_url | age | etcetera.....
一些想法? 我需要的是从users_info表中获取有关发送消息的用户的信息,例如照片,年龄。
答案 0 :(得分:0)
根据您的查询,您正在尝试为登录用户发送或接收上次聊天消息。
为此,
select c.*
from chat c
join users u on u.id = c.receiver_id or u.id = c.sender_id
where u.id = 'SESSION.User.Id'
order by c.id desc
答案 1 :(得分:0)
同时我找到了答案。这是:
select c.*, u.photo
from chat c
join users u on u.username = c.sender
WHERE (sender = '$login_session' AND receiver = '$uid') OR
(sender = '$uid' AND receiver = '$login_session')