在sql中显示某些信息(低于某个值)

时间:2011-08-04 22:39:47

标签: mysql sql database

我正在制作这个消息系统而且我遇到了麻烦。基本上,它是一个线程类型系统,有点像facebook风格的消息。用户1向用户75发送消息(消息1和线程1),并且用户75回复消息(消息2在线程1中创建)等等,依此类推。现在我能够完美地展示一切。

让我们说它达到了用户75在该线程上提交第5个回复的地步(消息13), 并且用户1删除消息以清理他的收件箱(NOW消息5具有“已删除”的值).....如果用户75回复,则该线程中的每个消息将再次出现给他。我希望它以某种方式工作,用户75的回复看起来像是用户1的新线程(但不是,我怎么能在sql中实现这一点) 这是用于在线程中显示消息的SQL

mysql>     SELECT message_id,thread_id,messages.user_id,to_id,messages.subject,messages.body,from_message_status,to_message_status,message_status,new,messages.date FROM messages 
    ->         INNER JOIN users ON users.id = messages.user_id
    ->         INNER JOIN messages_thread ON messages_thread.id = messages.thread_id
    ->         WHERE thread_id = 1
    ->         GROUP BY messages.message_id ORDER BY message_id ASC  ;
+------------+-----------+---------+-------+---------+---------------------------------------------------------------+---------------------+-------------------+----------------+-----+------------+
| message_id | thread_id | user_id | to_id | subject | body                                                          | from_message_status | to_message_status | message_status | new | date       |
+------------+-----------+---------+-------+---------+---------------------------------------------------------------+---------------------+-------------------+----------------+-----+------------+
|          1 |         1 |      75 |     1 |         | Wassup man.                                                   | unread              | unread            | deleted        |   0 | 1312493817 |
|          2 |         1 |       1 |    75 |         | im chilling , and you?                                        | unread              | unread            | read           |   0 | 1312493867 |
|          3 |         1 |      75 |     1 |         | Yea same same just posted man trying to find something to do. | unread              | unread            | deleted        |   0 | 1312493895 |
|          4 |         1 |      75 |     1 |         | what you trying to get into today?                            | unread              | unread            | deleted        |   0 | 1312493904 |
|          5 |         1 |      75 |     1 |         | just play some video games or something. you?                 | unread              | unread            | deleted        |   0 | 1312494046 |
|          6 |         1 |      75 |     1 |         | hello? dude?                                                  | unread              | unread            | read           |   0 | 1312494108 |
+------------+-----------+---------+-------+---------+---------------------------------------------------------------+---------------------+-------------------+----------------+-----+------------+

就像你在message_status中看到最后一次删除一样?我试图显示下面的所有内容

1 个答案:

答案 0 :(得分:2)

 SELECT ... FROM messages 
             INNER JOIN users ON users.id = messages.user_id
             INNER JOIN messages_thread ON messages_thread.id = messages.thread_id
             WHERE thread_id = 1 
             AND message_id > 
                (SELECT max(message_id) FROM messages 
                   WHERE thread_id = 1 and message_status = 'DELETED')
             GROUP BY messages.message_id ORDER BY message_id ASC  ;