Is there a specific way to select the oldest (or two oldest) dates from a column in MySQL?
是否有一种特定的方法可以从MySQL中的列中选择最早(或最早的两个)日期?
I imagine I would use the ordered by criterion.
我想我会按照标准排序。
3 个解决方案
#1
30
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, ...
等等,......
#2
19
Single oldest is easy:
单身老人很容易:
SELECT MIN(datefield) FROM yourtable
Oldest n
values requires a LIMIT query:
最旧的n值需要LIMIT查询:
SELECT datefield FROM yourtable ORDER By datefield ASC LIMIT n
#3
1
select MyDate from MyTable order by MyDate asc limit 2
#1
30
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, ...
等等,......
#2
19
Single oldest is easy:
单身老人很容易:
SELECT MIN(datefield) FROM yourtable
Oldest n
values requires a LIMIT query:
最旧的n值需要LIMIT查询:
SELECT datefield FROM yourtable ORDER By datefield ASC LIMIT n
#3
1
select MyDate from MyTable order by MyDate asc limit 2