Vimal
Vimal

Reputation: 1146

Get the nearest less than value

I need to get the nearest less than value from the table.. that is if my table is as follows

+----+----------+---------------------+
| id | display  | email               |
+----+----------+---------------------+
|  1 | 7        | [email protected]  |
+----+----------+---------------------+
|  2 | 1        | [email protected]  |
+----+----------+---------------------+
|  3 | 10        | [email protected] |
+----+----------+---------------------+
|  4 | 8        | [email protected]  |
+----+----------+---------------------+

I need to get the display which is less than 8.here there is 7 and 1.But i need only 7 which is the first less than value of 8.Please help me to find a logic to get this..

Upvotes: 0

Views: 48

Answers (3)

urfusion
urfusion

Reputation: 5501

You can use sorting.

SELECT * FROM TB WHERE display < 8 ORDER BY display DESC LIMIT 1

Upvotes: 0

Gurwinder Singh
Gurwinder Singh

Reputation: 39457

One way is using order by / limit

select *
from your_table
where display < 8
order by display desc
limit 1

Upvotes: 1

Barmar
Barmar

Reputation: 780724

Use the MAX() function to get the highest value that meets the criteria.

SELECT MAX(display)
FROM yourTable
WHERE display < 8

Upvotes: 0

Related Questions