按用户名获取最后一条消息对话

时间:2014-04-21 05:24:59

标签: php mysql

Plis帮助,获得最后的消息对话!!

数据库:

id,
user_from
user_to
msg

查询php

select * 
from messages 
where 
      user_from = 'Michael' AND 
      user_to = 'Daiana' OR 
      user_from = 'Daiana' AND 
      user_to = 'Michael' 
LEAST(user_to _id, user_from) 
order by id desc 
limit 1

请帮帮我。作为一个例子,他没有在两位Daiana和Michael用户的对话中列出最新消息。

示例结果:

  • daiana:你好,你好哟
  • michael:很好,你呢?
  • daiana:罚款:)

1 个答案:

答案 0 :(得分:0)

select * 
from messages 
where 
  (user_from = 'Michael' AND user_to = 'Daiana') 
  OR 
  (user_from = 'Daiana' AND user_to = 'Michael' )
LEAST(user_to _id, user_from) 
order by id desc 
limit 1