Just a little help if possible
I have a table with date data, I want to select dates that fall between today and 1 month ago, here's my query
SELECT id, stdate, DATE_SUB(CURDATE(), INTERVAL 1 month) as monthago, CURDATE() as today
FROM data_table
where (stdate between 'today' and 'monthago')
But this return nothing, also tried swapping the dates
SELECT id, stdate, DATE_SUB(CURDATE(), INTERVAL 1 month) as monthago, CURDATE() as today
FROM data_table
where (stdate between 'monthago' and 'today')
This
SELECT id, stdate, DATE_SUB(CURDATE(), INTERVAL 1 month) as monthago, CURDATE() as today
FROM data_table
returns the columns without an issue, what am I missing?
Also if I use:
SELECT id, stdate, DATE_SUB(CURDATE(), INTERVAL 1 month) as 'monthago', CURDATE() as 'today' FROM data_table where ('stdate' between 'monthago' and 'today')
it listed the entire table regardless of the stdate
A nested query must have an alias, so I added 'X' as the alias in your code:
select id, 'today', 'monthago', today, monthago
from (
SELECT id, stdate, DATE_SUB(CURDATE(), INTERVAL 1 month) as monthago, CURDATE() as today
FROM data_table X)
As regards your earlier question, in MySQL and other databases too, there is a difference between single quote (') and backquote (`). I think you meant to use the latter, which is what Ditto was getting at.
I'm an Oracle guy, not very familiar with MYSQL .. I was thinking his issue was the fact that:
where column = 'today'
is not the same as:
where column = today
if today is some internal function, then you probably shouldn't use single quotes ... if MYSQL uses back quotes ` .. then yeah, sure but I was making an assumption MYSQL treats single or double quotes as strings.
which would be like trying to match:
where Nov 25, 2014 = "today" ..
as opposed to:
where Nov 25, 2014 = Nov 25, 2014
(ie where date_col = today ) ??