Aleks
Aleks

Reputation: 45

mysql - how to select data from next month

i have table with id, date_from, date_to fileds. I need to find all the data where date_to +1 month is equal to condition

for example> if data in date_to field is 2015-06-01 and WHERE condition is 2015-07-01 i should get the result

SELECT *, (date_to + INTERVAL 1 MONTH) as 'next' FROM table_name WHERE 'next'='2015-07-01'

this is my query, that obviously doesn't work ;)

Tnx in adavance

Upvotes: 0

Views: 2226

Answers (1)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44874

First thing you can not use alias in the where condition and also the condition is wrong. It will be

SELECT * FROM table_name 
WHERE 
date_add(date_to,interval 1 month)='2015-07-01'

Now note that for large data-set its not a good idea to use function on the date filed since even if the field is index it will fail to use index. So better to use the function on the data to be filtered rather than on the field. So the above query could be written as

SELECT * FROM table_name 
WHERE 
date_to= date_sub('2015-07-01',interval 1 month)

Upvotes: 1

Related Questions