按另一个表列mysql排序

时间:2013-11-19 04:52:35

标签: mysql sql

我有两个表,messagesthread消息是主表,线程是我在两个用户之间保持对话的地方。我只想从messages表中选择,并从thread_id表中按thread排序。我正在使用下面的sql,但它没有按我的意愿订购。

我的目标是在thread表中插入新邮件时,messages表中的父邮件显示在顶部。有没有人有任何想法?

SELECT m.* 
FROM messages AS m 
     JOIN thread AS t 
          ON t.thread_id = m.id 
WHERE ( m.to_user = ?  
    AND m.deleted != ?
    AND m.del2 != ?)
    OR
    (m.from_user = ? 
     AND m.conversation = 'true'
     AND m.deleted != ? 
     AND m.del2 != ?) 
ORDER BY t.thread_id desc);

消息表

id      to_user  from_user      message      is_read     deleted     del2   conversation
----------------------------------------------------------------------------------------
1        user1     user2         hi mark,      true                         true
2        user3     user4         wass up yo?   true                         true
3        user1     user3         blah blah     

主题表

thread_id      thread_to_user  thread_from_user      thread_message     thread_message_id  thread_is_read
---------------------------------------------------------------------------------------------------

1              user2           user1               hi there,                    1            1
2              user1           user2               hey, wassup                  1            1
3              user2           user1               not much, hw u doin          1            1
4              user1           user2               doing great and you?         1            0
5              user3           user4               heyyyy                       2            1
6              user4           user3               hi, u coming?                2            0

在回显行时我期待的是什么:

id      to_user  from_user      message      is_read     deleted     del2   conversation
----------------------------------------------------------------------------------------
 2        user3     user4         wass up yo?   true                         true
 1        user1     user2         hi mark,      true                         true
 3        user1     user3         blah blah     

2 个答案:

答案 0 :(得分:3)

试试这个:

SELECT DISTINCT(id), to_user,from_user,
 message, is_read, conversation
FROM messages
LEFT JOIN (
  SELECT thread_id, thread_message_id
  FROM messages m
  LEFT JOIN thread t
  ON m.id = t.thread_message_id
) tbl
ON messages.id = tbl.thread_message_id
ORDER BY tbl.thread_id DESC

请参阅fiddle demo

您可以将WHERE放在子查询中,例如:

 SELECT DISTINCT(id), to_user,from_user,
  message, is_read, conversation
  FROM messages
  LEFT JOIN (
   SELECT thread_id, thread_message_id
   FROM messages m
   LEFT JOIN thread t
   ON m.id = t.thread_message_id
   WHERE ( m.to_user = ?  
   AND m.deleted != ?
   AND m.del2 != ?)
   OR
  (m.from_user = ? 
    AND m.conversation = true
    AND m.deleted != ? 
    AND m.del2 != ?) 
  ) tbl
  ON messages.id = tbl.thread_message_id
  ORDER BY tbl.thread_id DESC

请参阅Fiddle Demo with WHERE

更新(使用带有子查询的MAX()

SELECT id, to_user, from_user, message, is_read, conversation
FROM (
 SELECT m.id, max(t.thread_id) thread_id, m.to_user,m.from_user,m.message, m.is_read,    m.conversation
 FROM messages m
 LEFT JOIN thread t on(t.thread_message_id = m.id)
  WHERE ( m.to_user = 'user1'  
  AND m.deleted != true
 AND m.del2 != true)
 OR
 (m.from_user = 'user3' 
  AND m.conversation = true
  AND m.deleted != true 
  AND m.del2 != true) 
 GROUP BY m.id
 ORDER BY thread_id desc
 ) tbl

See Other Demo

答案 1 :(得分:0)

请参考以下查询和小提琴示例

select m.id, max(t.thread_id) thread_id, m.to_user,m.from_user,m.message, m.is_read, m.conversation
from messages m
left join thread t on(t.thread_message_id = m.id)
group by t.thread_message_id
order by thread_id desc;

fiddle example