Reputation: 1146
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
Reputation: 5501
You can use sorting.
SELECT * FROM TB WHERE display < 8 ORDER BY display DESC LIMIT 1
Upvotes: 0
Reputation: 39457
One way is using order by / limit
select *
from your_table
where display < 8
order by display desc
limit 1
Upvotes: 1
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