【问题标题】:I am converting Date in mysql query its not working with str_to_date?我正在转换 mysql 查询中的日期,它不能与 str_to_date 一起使用?
【发布时间】:2015-11-10 18:51:46
【问题描述】:

我在 Mysql 查询中转换日期。它以 2015 年 1 月 10 日的格式保存在数据库中的 varchar 中。我将其转换为年月日 2015-01-10。 我使用了str_to_date(myfield, '%d-%M-%Y')date_format(myfield, '%d-%M-%Y'),,但它没有显示任何行。

我正在编写的查询没有显示任何行,但当我在字符串中给出确切日期时它仍然有效。

SELECT * FROM table where str_to_date(date, '%d-%M-%Y') = '20-June-2015';

【问题讨论】:

  • 您最好更改架构并将正确的类型存储在date列中

标签: php mysql


【解决方案1】:

使用mysqlDate_Format

Date_Format(date, '%d-%M-%Y')

使用类似 taht 的查询:-

SELECT * FROM table where Date_Format(date, '%d-%M-%Y') = '20-June-2015';

或者像这样使用str_to_date :-

SELECT * FROM table where date= str_to_date('20-June-2015', '%d-%M-%Y')

【讨论】:

    【解决方案2】:

    有趣的答案,但我想它是正确的:-) :

    由于它保存在 var char 中,因此您不需要任何转换函数:

    SELECT * FROM table where `date` = '20-June-2015';
    

    【讨论】:

    • @FirstOne 当然不会。但为什么呢?
    猜你喜欢
    • 2021-12-29
    • 2015-12-15
    • 2014-06-28
    • 2016-03-02
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 2020-03-21
    • 2016-10-19
    相关资源
    最近更新 更多