简体   繁体   中英

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. 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. 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. How do I form an sql that will accomplish this?

PS: Symfony "createQueryBuilder" -example for this would be great, but not necessary! :)

Edit: (More info) This table has other columns as well, 10 others to be exact, including id-column as well. 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
              );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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