php - mysql 查询以获得下一个结果

标签 php mysql

在某些条件下如何在 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/

相关文章:

php - 使用 MySQL 和 PHP 进行多个下拉选择

php - Laravel/PHP 中的 "/"和 "\"有什么区别?

php - php中两个城市之间的距离

php - 如果我对名称中带有方括号的输入使用 jQuery serialize(),为什么会出现此分号?

php - ajax+php+mysql : (another) issue with charsets

mysql - 创建自定义论坛,帮助构建查询以计算类别中的帖子等

mysql - 在 MySQL 的关系表中搜索 2 次出现

php - 如何在不使用多个查询语句的情况下将特定的 dbs 值分配给多个变量?

php - 在 android 中获取 HTML 响应而不是 JSON

mysql - 查找所有使用相同零件类型的汽车