Select rows after a row with a certain value

懵懂的女人 提交于 2019-12-10 15:22:12

问题


Hey again, im still getting a hang of Queries and stuff so please excuse all the frequent sql questions 8)

Anyways im trying to select rows after a certain value. Dont understand? Ok this is how it actually looks.

Table: messages

+------------+-----------+---------+-------+---------------------------------------+----------------+------------------+
| message_id | thread_id | user_id | to_id | body                                  | message_status | uid_sent_deleted |
+------------+-----------+---------+-------+---------------------------------------+----------------+------------------+
|          1 |         1 |       1 |     7 | How are you bro?                      | read           |                1 |
|          2 |         1 |       7 |     1 | IM good what about you kenny?         | read           |                0 |
|          3 |         1 |       1 |     7 | Same just chilling how is your sister | read           |                1 |
|          4 |         1 |       7 |     1 | Shes coool u know just doin great     | read           |                0 |
|          7 |         1 |       1 |     7 | Thats nice                            | read           |                1 |
|          8 |         1 |       7 |     1 | Yupp                                  | read           |                0 |
|          9 |         1 |       1 |     7 | hhahaha                               | read           |                1 |
|         10 |         1 |       7 |     1 | anyways....                           | unread         |                0 |
+------------+-----------+---------+-------+---------------------------------------+----------------+------------------+

Where the message id is 9, is has the value of '1' in the column uid_sent_deleted. Im trying to select rows after the last row that has a value of '1' in the column uid_sent_deleted.

here is the sql that im currently working with. Thanks for the help!!

SELECT message_id, thread_id, messages.user_id, to_id, body, message_status, uid_sent_deleted
FROM messages
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 

回答1:


... WHERE thread_id = 1 AND id > 
     (SELECT MAX(id) FROM messages WHERE uid_sent_deleted = 1)



回答2:


SELECT message_id, thread_id, messages.user_id, to_id, body, message_status, uid_sent_deleted
FROM
    (SELECT thread_id t_id, MAX(message_id) m_id
     FROM messages
     WHERE thread_id =1 and uid_sent_deleted = 1
     GROUP BY thread_id) n
inner join messages on messages.message_id > n.m_id and messages.thread_id = n.t_id
INNER JOIN messages_thread ON messages_thread.id = messages.thread_id
ORDER BY message_id ASC
LIMIT 1



回答3:


A subquery might work, you select the MAX(message_id) WHERE uid_sent_deleted=1, then do you join ... little hazy on the syntax.



来源:https://stackoverflow.com/questions/5126116/select-rows-after-a-row-with-a-certain-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!