在where子句MySQL中转换日期

时间:2020-08-01 18:49:41

标签: mysql where-clause date-format

我希望将日期从'd / m / Y'转换为'Y / m / d' inside where子句,如下所示:

Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request

Reason: Error reading from remote server

Apache/2.4.25 (Debian) Server at 192.168.1.250 Port 80

此查询始终返回空结果

有办法做到吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

只需将您的数据转换为日期,然后格式化

SELECT * FROM myTable WHERE DATE_FORMAT(STR_TO_DATE(myDate,'%d/%m/%Y'),'%Y/%m/%d') = '2020/08/01'

或者将它们都设为日期,然后可以使用日期功能

SELECT * FROM myTable WHERE STR_TO_DATE(myDate,'%d/%m/%Y') = STR_TO_DATE('2020/08/01','%Y/%m/%d')

最好是将日期另存为Date / Datetime / timestamp,这样您就可以毫无问题地进行计算,并且始终可以将它们转换为任何输出

相关问题