【问题标题】:Date format conflict日期格式冲突
【发布时间】:2017-10-31 05:44:07
【问题描述】:

我的表中有一个字段“joindate”。它由混合格式的数据组成。 (2015 年 1 月 14 日和 2015 年 1 月 14 日)。我想按日期的 desc 顺序从该表中获取数据。我怎样才能做到这一点 ? joindate 的类型是 varchar。 我尝试过以下方式;

使用 str_to_date 将该字符串转换为日期。但它仍然无法正常工作。

    select * from employee where employeeid='14' order by str_to_date(joindate,'%d.%m.%Y') desc;

【问题讨论】:

  • 在此处发布您的查询
  • 但也有其他格式
  • select DATE_FORMAT(date, '%m-%d-%Y') as date from employee where employeeid='14' order by date desc;
  • 我尝试过使用 date_format。但没用
  • 仅供参考:作为最佳实践,始终将日期保存为一种格式。据我所知,MySQL 使用 - 作为日期,格式为 YYYY-mm-dd

标签: php database date


【解决方案1】:

你可以试试这个查询:

select * from employee where employeeid='14' order by if(strcmp(str_to_date(datejoin,'%d.%m.%Y'),datejoin),datejoin,date_format(str_to_date(datejoin,'%d-%m-%Y'),'%d.%m.%Y')) desc;

【讨论】:

    【解决方案2】:

    试试date_format 它对我有用

     select * from employee where employeeid='14' order by date_format(str_to_date(joindate,'%d-%m-%Y'), '%d.%m.%Y') desc;
    

    【讨论】:

    • 它仍然给出混合日期的输出,而不是按 desc 顺序输出
    • 尽可能分享你的输出
    【解决方案3】:

    如果 joindates 在数据库中,那么您可以使用

    SELECT * FROM employee WHERE employeeid = '14' ORDER BY joindate DESC
    

    是这样还是你应该去掉那些百分比字符?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-22
      • 2020-04-16
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 2011-12-21
      • 2016-08-03
      相关资源
      最近更新 更多