Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select oldest date from MySQL

Tags:

Is there a specific way to select the oldest (or two oldest) dates from a column in MySQL?

I imagine I would use the ordered by criterion.

like image 778
Steven Matthews Avatar asked Nov 14 '11 18:11

Steven Matthews


People also ask

How do I select the oldest date in SQL?

First, create an aggregate query that has two fields: GroupID and RecordDate. Group by the GroupID field, and choose the "Min" option for the RecordDate, to return the earliest date for each query.

How do I find the oldest employee in SQL?

Answer: Use a nested query to find the earliest birthday. We can order by birthday and select the first one only. If there are two or more oldest people then only one of them will be returned.

What is dob in MySQL?

MySQL uses yyyy-mm-dd format for storing a date value. This format is fixed and it is not possible to change it. For example, you may prefer to use mm-dd-yyyy format but you can't. Instead, you follow the standard date format and use the DATE_FORMAT function to format the date the way you want.


1 Answers

You can order by the date field in your database. For oldest:

SELECT * FROM table WHERE condition ORDER BY dateField ASC LIMIT 1 

For two oldest:

SELECT * FROM table WHERE condition ORDER BY dateField ASC LIMIT 2 

etc, etc, ...

like image 54
Logan Serman Avatar answered Oct 18 '22 13:10

Logan Serman