MySQL ADDDATE,DATE_FORMAT和CONCAT_WS

时间:2013-01-27 06:10:07

标签: mysql sql date select

我正在构建以下Query但是遇到语法问题:

SELECT FORMAT(yrs_served * 365, 0) AS days_served,
ADDDATE(date_comm, INTERVAL days_served DAY(DATE_FORMAT(CONCAT_WS('-', yr_comm, mth_comm, day_comm), %Y-%m-%d) AS date_comm) AS left_office
FROM prime_minister JOIN ministry
;

可以看出,我首先尝试将三个值CONCAT_WS转换为日期,然后将其格式化为DATE_FORMAT的日期,以便我可以使用ADDDATE将天数添加到此日期。

请告诉我哪里出错了,谢谢!

1 个答案:

答案 0 :(得分:2)

您不能在已定义的同一级别上使用ALIAS

SELECT   FORMAT(yrs_served * 365, 0) AS days_served,
         ADDDATE(CONCAT_WS('-', yr_comm, mth_comm, day_comm), INTERVAL (yrs_served * 365) DAY) AS left_office
FROM     prime_minister JOIN ministry.....

如果您不想使用它,只需将其包装在子查询中,例如

SELECT  days_served,
        ADDDATE(date_comm, INTERVAL days_served DAY) AS left_office
FROM
(
    SELECT  FORMAT(yrs_served * 365, 0) AS days_served,
            CONCAT_WS('-', yr_comm, mth_comm, day_comm) as date_comm
    FROM     prime_minister JOIN ministry.....
) s