首页 > 解决方案 > mysql date_format() 或 str_to_date() 在 where 子句中

问题描述

我已经查看了名为“销售视图”的视图

select * from SalesView;

在此处输入图像描述

我想从 SalesView 中获取 order_date 为 9-OCT-2019 的记录。订单日期的值由 GUI 以 DD-MM-YYYY 格式提供。

我试过了,

select * 
from salesView 
where str_to_date(order_date,'%d-%m-%Y') = str_to_date('09-10-2019','%d-%m-%Y')
order by oID;

在此处输入图像描述

我也尝试过 date_format() 而不是 str_to_date(),但它什么也没返回。

标签: mysqlsql

解决方案


检查你的 where 子句

where str_to_date(order_date,'%d-%m-%Y') = str_to_date('09-10-2019','%d-%m-%Y')

将 order_date 的格式替换为数据库存储它的格式。YYYY-MM-DD

where str_to_date(order_date,'%Y-%m-%d') = str_to_date('09-10-2019','%d-%m-%Y')

推荐阅读