Query to select maximum of all minimum
I want to extract a row, with the max value in one field but is less than a certain value of that value;
Fore example,
SELECT id,max(val) WHERE val<23
Here, I need the value of the maximum of field val
but which must be less than 23. This works and gives me, the maxi开发者_StackOverflow中文版mum values, but the id
field is not respective to that field, it provides the id of the first occurence.
What is the right query?
How about:
SELECT id, val FROM table WHERE val < 23 ORDER BY val DESC LIMIT 1
Try this:
SELECT ID, VAL
FROM [TABLE]
WHERE VAL = (SELECT MAX(VAL) FROM [TABLE] WHERE VAL < 23)
精彩评论