Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql DATE_ADD INTERVAL with mysql table fields

Tags:

php

mysql

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.

like image 568
Dipesh Parmar Avatar asked Aug 22 '12 07:08

Dipesh Parmar


People also ask

How can I add 15 days to current date in MySQL?

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.

What is interval in MySQL?

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.

What is Adddate in MySQL?

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.


1 Answers

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
like image 104
valex Avatar answered Sep 28 '22 18:09

valex