How to select oldest date from MySQL

后端 未结 4 1353
忘掉有多难
忘掉有多难 2020-12-18 19:21

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.

4条回答
  •  Happy的楠姐
    2020-12-18 19:34

    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

    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 .

提交回复
热议问题