STR_TO_DATE为列,但未找到列

时间:2018-03-19 10:41:46

标签: mysql sql

此查询错误:

select *, STR_TO_DATE(start, '%d/%m/%Y') as date_format from dates where date_format >= 2018-03-19 

错误:

Column not found: 1054 Unknown column 'date_format' in 'where clause'

3 个答案:

答案 0 :(得分:3)

您不能在where子句中使用列别名。 MySQL有一个扩展,您可以在having子句中执行此操作(不进行任何聚合)。所以你可以这样做:

select d.*, STR_TO_DATE(start, '%d/%m/%Y') as date_format
from dates d
having date_format >= '2018-03-19';

通常的建议是重复表达式:

select d.*, STR_TO_DATE(start, '%d/%m/%Y') as date_format
from dates d
having STR_TO_DATE(start, '%d/%m/%Y') >= '2018-03-19';

但是,我强烈建议您更改表的结构。日期不应存储为字符串。您可以轻松解决此问题:

update dates
    set start = STR_TO_DATE(start, '%d/%m/%Y');

alter table dates modify column start date;

答案 1 :(得分:0)

您不能使用date_format,因为它只是名称而是STR_TO_DATE(start, '%d/%m/%Y')

答案 2 :(得分:0)

试试这种方式

start不是字段,因此请使用date_format代替select *, STR_TO_DATE(start, '%d/%m/%Y') as date_format from dates where start >= 2018-03-19

editMessageReplyMarkup