MySQL ADDDATE, DATE_FORMAT and CONCAT_WS

I create the following query, but I have syntax problems:

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 ; 

As I could see, I first try to enter the three CONCAT_WS values ​​on the date, and then format it as a date with DATE_FORMAT so that I can then use ADDDATE to add the number of days to this date.

Please let me know where I am wrong, thanks!

+4
source share
1 answer

you cannot use ALIAS at the same level that it defined.

 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..... 

If you do not want to use it, you just wrap it in a subquery, for example

 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 
+2
source

All Articles