I have one table called datas in there I have id
,start_date
,end_date
,day_count
,day_type
.
Where id
is primary key,start and end_date
is datetime, day_count
is int and day_type
is varchar.
Now day_type store DAY,WEEK,MONTH as value when user made request and day field hold number of days like 1 to 60.
Now I want to user this count and type in date_add mysql built-in function.
I have tried to pass it as below but its show error.
SELECT
datas.day_count,
datas.day_type,
MIN( datas.start_date ) AS MinDate,
MAX( datas.end_date ) AS MaxDate,
DATE_ADD(MaxDate,INTERVAL datas.day_count datas.day_type) AS ExactDate,
datas.trade_service_id
FROM datas
Erro is
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'datas.day_type)) AS ExactDate, datas' at line 13
Please little hint would be great.
Use the DATE_ADD() function if you want to increase a given date in a MySQL database. In our example, we increased each start date by two days.
MySQL interval is an operator, which is based on the binary search algorithm to search the items and returns the value from 0 to N. It is mainly used to calculate the date and time values. We can use the following syntax to create an interval value: INTERVAL expr unit.
ADDDATE() function in MySQL is used to add the specified intervals to the given date and time. It returns the date or DateTime after adding the interval.
This query works in MySQL but what do you exactly want to output without Group and so on?
SELECT
datas.day_count,
datas.day_type,
MIN( datas.start_date ) AS MinDate,
MAX( datas.end_date ) AS MaxDate,
start_date, min(start_date) as min_date,
case day_type
when 'MONTH' then DATE_ADD(MAX( datas.end_date ),INTERVAL datas.day_count MONTH)
when 'DAY' then DATE_ADD(MAX( datas.end_date ),INTERVAL datas.day_count DAY)
end as ExactDate,
datas.trade_service_id
FROM datas
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With