I want to execute one mysql statement where I have a select that is not part of the where condition.
this is my code:
SELECT oil.empid
, oil.mileage
, oil.`date`
, max(oil.mileage) AS expr1
FROM
oil
WHERE
oil.mileage < (SELECT max(oil.mileage) AS expr1
FROM
oil)
GROUP BY
oil.empid
ORDER BY
oil.mileage
now this selects the second largest mileage but what i want is both the second and largest mileage. I believe that i just need to select max(mileage) outside of the WHERE but im not sure how to do that. Thanks for any help
edit:
If the data entered for these 1497 was
mileage: 100 , 100000 , and 200000
and for empid 2000
mileage: 100 , 1500 , 5000
I would want this:
empid|2nd max | max
1497 | 100000 | 200000
2000 | 1500 | 5000
You can use
LIMITwithOFFSETin MySQL’sORDER BYclause to find the second mileage. This way you gain by not using the expensiveGROUP BY.(Update) after the clarifications by the OP: