4

我正在构建以下查询,但遇到语法问题:

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 将天数添加到该日期。

请指教我哪里出错了,谢谢!

4

1 回答 1

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
于 2013-01-27T06:30:25.740 回答