简体   繁体   English

SQL-查找值相同或下一个最大值的所有行

[英]SQL - find all the rows where the values are the same, or next biggest

I have a table where in one column I have lot of Int -values from x to y and it kinda loops. 我有一张表,其中一列中有很多从x到y的Int值,它有点循环。 The values go from lowest to highest, but start again after awhile. 值从最低到最高,但过一会又重新开始。 Values look like this: 值如下所示:

TableRow:
2
5
10
15
..
30
40
50
// And here it begins again:
2
5
10
..
// And again:
2
5
10
// And the sequence may change too:
3
6
10

I would like to search for values that are the same, or next biggest. 我想搜索相同或下一个最大值的值。 Ie If I have a search value of 6, I would like the search to return rows where this value is: 10, 10, 10, and so on. 即,如果我的搜索值为6,我希望搜索返回该值为10、10、10等的行。 Not the fives, but the bigger ones. 不是五岁以下儿童,而是五岁以下儿童。 But I don't want all of the bigger ones after the value! 但是我希望所有的大公司都值钱! Only the next one. 只有一个。 If I have a search value of 1.9 or less, the search should return all the 2 -values. 如果我的搜索值为1.9或更小,则搜索应返回所有2值。 How do I form an sql that will accomplish this? 如何形成将完成此操作的sql?

PS: Symfony "createQueryBuilder" -example for this would be great, but not necessary! PS:Symfony“ createQueryBuilder”-这个示例很好,但不是必须的! :) :)

Edit: (More info) This table has other columns as well, 10 others to be exact, including id-column as well. 编辑:(更多信息)该表也有其他列,确切地说还有10列,包括id列。 No timestamps though. 虽然没有时间戳。

You can find the value you are looking for using a subquery. 您可以使用子查询找到要查找的值。 Then you can get the matching rows: 然后,您可以获得匹配的行:

select t.*
from table t
where value = (select min(value)
               from table t
               where value >= 6
              );

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

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