Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8139391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:48:35+00:00 2026-06-06T11:48:35+00:00

I want to execute one mysql statement where I have a select that is

  • 0

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:48:36+00:00Added an answer on June 6, 2026 at 11:48 am

    You can use LIMIT with OFFSET in MySQL’s ORDER BY clause to find the second mileage. This way you gain by not using the expensive GROUP BY.

    (Update) after the clarifications by the OP:

    SELECT oil.empid
         , oil.mileage
         , oil.date
    FROM
            oil
        JOIN
            ( SELECT DISTINCT empid
              FROM oil
            ) AS emp
                ON  oil.empid = emp.empid
                AND oil.mileage >= 
                    ( SELECT mileage
                      FROM oil AS o
                      WHERE o.empid = emp.empid
                      ORDER BY mileage DESC
                      LIMIT 1  OFFSET 1
                    )
    ORDER BY
        oil.empid
      , oil.mileage               --- ASC or DESC here, your choice
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have asp.net generated html code. I want execute jQuery click function when user
I want to execute this script (view source) that uses Google Translate AJAX API
So I have this function that takes all the users from an MySql table
I have a MySQL table like this one: day int(11) hour int(11) amount int(11)
I want to issue multiple mysql commands with one mysql_query function. This is my
I want to execute two select queries in one go, but the data isn't
I want to execute some Python code, typed at runtime, so I get the
I want to execute a query like this var result = from entry in
I want to execute a query inside a db.sql file using MySQL 5.1 in
So the scenario is this: I have a mySQL database on a local server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.