如何从 MySQL 中选择最旧的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8125996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select oldest date from MySQL
提问by Cecil Rodriguez
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.
我想我会使用按标准排序。
回答by Logan Serman
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, ...
等等等等……
回答by Marc B
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
回答by abcde123483
select MyDate from MyTable order by MyDate asc limit 2
回答by Alfarouq
if the column type is date or timedate
如果列类型是日期或时间日期
SELECT * FROM tablename WHERE 1 ORDER BY datecolumn ASC LIMIT 2
if the column type is text or varchar
如果列类型是 text 或 varchar
SELECT *,DATE(STR_TO_DATE(datecolumn , '%H:%i %d-%m-%Y' )) AS columnname FROM tablename WHERE 1 ORDER BY columnname ASC LIMIT 2
You can choose your date format from here.
您可以从这里选择您的日期格式。