A PHP script returns a value which must be looked up in the first column of a table in a MySQL database.
If the exact value returned by the script appears in that first table column, then the query needs to extract the further information from the other columns along that row.
If the script value doesn't appear in the first table column, then the same information should be extracted from the next lowest value, should it exist.
So the database has rows looking like this, with the target lookup value in the first column:
18 0.3783 0.4438 0.5155 0.5614 0.6787
19 0.3687 0.4329 0.5034 0.5487 0.6652
20 0.3598 0.4227 0.4921 0.5368 0.6524
25 0.3233 0.3809 0.4451 0.4869 0.5974
30 0.2960 0.3494 0.4093 0.4487 0.5541
If the returned script value is 25
, then the five other other values in the same row:
0.3233 0.3809 0.4451 0.4869 0.5974
...should be extracted.
If the script value however is 24
, then the row information with the next lowest value needs to be extracted, which would be that for the database first-column value 20
, being:
0.3598 0.4227 0.4921 0.5368 0.6524
What sort of query would be best suited for this?
You can filter on rows whose first column is equal to or less than the parameter, order by
descending value in that column, and keep the top row only, using limit
.
Assuming that the filtering column is called id
:
select *
from mytable
where id <= ?
order by id desc
limit 1
The question mark represents the parameter to the query.