在某些条件下如何在 mysql 中获得接下来的几个结果?
+-----+---------------+-------------------------+
| id | update_type | message |
+-----+-----------------------------------------+
| 1 | 10 | One tow three |
| 2 | 20 | No error in this |
| 3 | 0 | My testing message |
| 4 | 0 | php module test |
| 5 | 30 | hello world |
| 6 | 0 | team spirit |
| 7 | 40 | puzzle game |
| 8 | 50 | social game |
| 9 | 0 | stackoverflow |
|10 | 0 | stackexchange |
+-----+---------------+-------------------------+
在上表中,如何获取从 update_type=20
到 update_type 更改为 30
的所有消息。 (上表是静态的,值为 0
,并且可以扩展到许多下一行)
最佳答案
您可以使用子查询来实现此目的。假设每个值出现一次:
select t.*
from table t
where t.id >= (select t2.id from t t2 where t2.update_type = 20) and
t.id <= (select t2.id from t t2 where t2.update_type = 30);
如果这些值出现多次,则此查询将生成错误(子查询预计仅返回一个值)。在这种情况下您想要做什么尚不清楚,但子查询中的 min()
或 max()
可以提供帮助。
关于php - mysql 查询以获得下一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34000943/