MySQL: Select rows with values nearest but greater than a given value

pandron

Okay, I'm struggling here. I have a MySQL table of unsorted values that is structured about like this:

ID     Size
-----  -----
1      12
2      10
3      1
4      0
5      8
6      9
7      6
8      2
9      9
10     4

What I'd like to do is given a size (e.g. size = 5) extract the next bigger rows (and the next smaller eventually).

I've tried:

SELECT * FROM Table WHERE Size > 5 ORDER BY Size DESC LIMIT 2

But that gives me:

ID     Size
-----  -----
1      12
2      10

but what I want is

ID     Size
-----  -----
7      6
5      8

Any help would be appreciated. I'm using PHP to access the table if that makes a difference. Thanks!

shubham715

You are using DESC that fetching result in descending . you should use ASC that will fetch results in ascending.

Example-

SELECT * FROM Table WHERE Size > 5 ORDER BY Size ASC LIMIT 2

EDITED

You need to first fetch in descending order then you need to again order by ascending.

Example-

  SELECT * FROM
    (SELECT * FROM Table WHERE Size > 5 ORDER BY Size DESC ) AS r
ORDER BY r.Size ASC LIMIT 2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL - Select rows with values greater than max value for id and category

From Dev

mysql value greater than percent of number of values

From Dev

Waterline (Sails.js). Rows with 'updatedAt' greater than a given value

From Dev

Waterline (Sails.js). Rows with 'updatedAt' greater than a given value

From Dev

MySQL delete all rows where id is greater than the given number

From Dev

MySQL count rows where column value is same and select them where count is greater than 2

From Dev

Matplotlib histogram - plotting values greater than a given value

From Dev

Plot only values greater than given value: ggplot R

From Dev

Compare rows in db and select if value is greater than 15%

From Dev

MySQL - Select rows greater than timestamp AND specific UUID?

From Dev

mysql select if count of values for each user greater than 1

From Dev

MySQL: Select TIMEDIFF values where the difference is greater than x

From Dev

Find the nearest number greater than a specific value among cells

From Dev

MySQL Select where column greater than or equal to closest past date from given date

From Dev

MySQL select rows with two where conditions and count greater than 1 with same column ID

From Dev

MySQL select rows with two where conditions and count greater than 1 with same column ID

From Dev

Selecting entries with nearest value less than given value

From Dev

disable/ remove value if select 1 is greater than select 2 without mysql

From Dev

Select all columns greater than some value

From Dev

Select where no value is greater than X

From Dev

Select all columns greater than some value

From Dev

MYSQL Select Records Greater Than OR First

From Dev

Mysql Select datetime, greater than timestamp

From Dev

MySQL select records with sum greater than threshold

From Dev

MYSQL Select Count if greater than 0

From Dev

MySQL how to select the nearest values to my variables

From Dev

postgresql: how to get the rows greater than a timestamp and also previous nearest row

From Dev

Excel: Select the cell to the left that is greater than/less than some value

From Dev

Smallest sum of subarray with sum greater than a given value

Related Related

  1. 1

    SQL - Select rows with values greater than max value for id and category

  2. 2

    mysql value greater than percent of number of values

  3. 3

    Waterline (Sails.js). Rows with 'updatedAt' greater than a given value

  4. 4

    Waterline (Sails.js). Rows with 'updatedAt' greater than a given value

  5. 5

    MySQL delete all rows where id is greater than the given number

  6. 6

    MySQL count rows where column value is same and select them where count is greater than 2

  7. 7

    Matplotlib histogram - plotting values greater than a given value

  8. 8

    Plot only values greater than given value: ggplot R

  9. 9

    Compare rows in db and select if value is greater than 15%

  10. 10

    MySQL - Select rows greater than timestamp AND specific UUID?

  11. 11

    mysql select if count of values for each user greater than 1

  12. 12

    MySQL: Select TIMEDIFF values where the difference is greater than x

  13. 13

    Find the nearest number greater than a specific value among cells

  14. 14

    MySQL Select where column greater than or equal to closest past date from given date

  15. 15

    MySQL select rows with two where conditions and count greater than 1 with same column ID

  16. 16

    MySQL select rows with two where conditions and count greater than 1 with same column ID

  17. 17

    Selecting entries with nearest value less than given value

  18. 18

    disable/ remove value if select 1 is greater than select 2 without mysql

  19. 19

    Select all columns greater than some value

  20. 20

    Select where no value is greater than X

  21. 21

    Select all columns greater than some value

  22. 22

    MYSQL Select Records Greater Than OR First

  23. 23

    Mysql Select datetime, greater than timestamp

  24. 24

    MySQL select records with sum greater than threshold

  25. 25

    MYSQL Select Count if greater than 0

  26. 26

    MySQL how to select the nearest values to my variables

  27. 27

    postgresql: how to get the rows greater than a timestamp and also previous nearest row

  28. 28

    Excel: Select the cell to the left that is greater than/less than some value

  29. 29

    Smallest sum of subarray with sum greater than a given value

HotTag

Archive