how to select max and min?

MIN() and MAX() in mysql returing wrong values?

  • I have a table in MYSQL to track movies. The two fields I am interested in at this time are mlength (INT) (entered in minutes i.e. 98) and Name. When I query to find the MIN time it works ok: SELECT MIN(length) FROM movie; If I add this: SELECT mname, MIN(length) FROM movie; I get the same movie name whether I use MIN or MAX and the movie name I get is neither the shortest or longest movie in the database. I would ultimatley like to query to find the longest AND shortest film in the database (in a single query). Any thoughts on the syntax here? It looks correct to me. Thanks.

  • Answer:

    From what I understand you need to get either the name of the minimum or maximum entry length from the database? It'd be something more like: "SELECT mname FROM movie ORDER BY length ASC" That would output all of the movie names ordered by length. "I would ultimatley like to query to find the longest AND shortest film in the database (in a single query)." for this you would need something like: "SELECT name FROM movie WHERE length = (SELECT MAX(length) FROM movie) AND length = (SELECT MIN(length) FROM name) LIMIT 2"

Joe Smith at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.