简体   繁体   English

mysql查询以获得下一个结果

[英]mysql Query to get next results

How can I get next few results in mysql with some condition? 在某些情况下,如何在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           |
+-----+---------------+-------------------------+

In the above table how can I get all messages from update_type=20 and till update_type changes to 30 . 在上表中,如何从update_type=20获取所有消息,直到update_type更改为30 (Above table is static and value of 0 and can expand upto many next rows) (上表是静态的,值为0并且可以扩展到很多下一行)

You can use subqueries for this purpose. 您可以为此使用子查询。 Assuming each value occurs once: 假设每个值出现一次:

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);

If the values appear more than once, then this query will generate an error (the subquery is expected to return only one value). 如果值出现多次,则此查询将生成错误(子查询应仅返回一个值)。 What you want to do in this case is not clear, but min() or max() in the subqueries can help. 在这种情况下,您想要做什么并不清楚,但是子查询中的min()max()可以提供帮助。

If I did get your question rigt, you are looking for a solution in SQL. 如果确实遇到了问题,那么您正在寻找SQL解决方案。 If that's the point, then you can make a condition in the where-clause. 如果那是重点,那么您可以在何处建立条件。 ie

SELECT * FROM tablename WHERE columnname BETWEEN 20 AND 30;

You have to replace tablename with the name of your table. 您必须将tablename名替换为tablename名。 and columnname with your corresponding column in the table. columnname以及表格中的相应列。 Then you can enter the WHERE in the statement. 然后,您可以在语句中输入WHERE In your case the condition is BETWEEN . 您的情况是BETWEEN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM