Timo Huovinen
Timo Huovinen

Reputation: 55633

MySQL get mindate and maxdate in one query

How to get the maxmum date and minimum date in mysql using only one sql query?

Upvotes: 36

Views: 65640

Answers (4)

TomC
TomC

Reputation: 400

Very similar to Icarus's answer, but I prefer to rename the columns to something meaningful like:

SELECT MIN(date) AS start, MAX(date) AS finish FROM table

Upvotes: 1

Icarus
Icarus

Reputation: 63966

SELECT MIN(date_col), MAX(date_col) FROM table_name

Upvotes: 57

Joseph Nields
Joseph Nields

Reputation: 5661

JUST IN CASE someone came here looking for minimum and maximum supported dates like I did... here's the answer to your question :)

select 
    DATE('1000-01-01') MinDate, 
    DATE('9999-12-31') MaxDate

+------------+------------+
| MinDate    | MaxDate    |
+------------+------------+
| 1000-01-01 | 9999-12-31 |
+------------+------------+

Reference: https://dev.mysql.com/doc/refman/5.5/en/datetime.html

Upvotes: 23

aF.
aF.

Reputation: 66697

Then do it like said here:

SELECT MIN(CAST(date_col AS CHAR)), MAX(CAST(date_col AS CHAR)) FROM table_name

Upvotes: 6

Related Questions