Can I use an alias create with the "CASE ... WHEN" (delayDate), in another function, like date_add() :
doctrine way :
->addSelect("CASE c.time
WHEN '24' then 1
WHEN '48' then 2
WHEN '72' then 3
WHEN '96' then 4
ELSE 0
END
as delayDate ,
date_add(CURRENT_DATE(), delayDate, 'DAY') as firstDateDelivery")
This because I can't use 'HOUR' in doctrine with the date_add() function.
I think, in "pure" mysql, it's not working anymore...
Can you help me please ?
F.
Remove single quote from DAY, and check this DATE_ADD() usage func_date_add.
->addSelect("CASE c.time
WHEN '24' then 1
WHEN '48' then 2
WHEN '72' then 3
WHEN '96' then 4
ELSE 0
END
as delayDate ,
CASE c.time
WHEN '24' then date_add(CURRENT_DATE(), INTERVAL 1 DAY)
WHEN '48' then date_add(CURRENT_DATE(), INTERVAL 2 DAY)
WHEN '72' then date_add(CURRENT_DATE(), INTERVAL 3 DAY)
WHEN '96' then date_add(CURRENT_DATE(), INTERVAL 4 DAY)
ELSE CURRENT_DATE()
END
as firstDateDelivery")
I want to get data for the dates between 2015-05-01 and 2015-06-01 using SQL.
Please help me with the query.
The query I used is:
select *,count(id) as multiple_visitors
from table1
where id=123
and (date(server_time) between (CURDATE() - INTERVAL 31 DAY) AND CURDATE())
group by user_id having count(id)>1
You can do this with month() and year():
where month(server_time) = month(curdate() - interval 1 month) and
year(server_time) = year(curdate() - interval 1 month)
However, I recommend a slightly more complex expression:
where server_time >= date_sub(date_sub(curdate(), interval - day(curdate()) + 1 day), interval 1 month) and
server_time < date_sub(curdate(), interval - day(curdate()) + 1 day)
The advantage is that there are no functions on server_time, so the database engine can use an index, if appropriate.
As a note: the expression date_sub(curdate(), interval - day(curdate()) + 1 day) gets midnight on the first day of the month.
Try using "WHERE" with MONTH(date).
Like this:
SELECT * FROM Table
WHERE MONTH(date) = 1
here is the code I am using to return past 24 hours records
SELECT *
FROM mytable
WHERE CASE WHEN `created` > DATE_SUB(NOW(), INTERVAL 1 DAY) THEN 1 ELSE 0 END
how to return records between yesterday and last 7 days
Use the BETWEEN operator.
CASE WHEN created BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND DATE_SUB(NOW(), INTERVAL 1 DAY)
THEN 1
ELSE 0
END
Try this, it works without case statement, so should be faster:
SELECT *
FROM mytable
WHERE created BETWEEN date(CURRENT_TIMESTAMP-7) AND date(CURRENT_TIMESTAMP-1);
Is it possible to use a function or a variable as the label for a column in MySQL?
So for example if you wanted to include the date, something like:
select
sum(P.AMOUNT) AS monthname(date_sub(CURDATE(), INTERVAL 1 MONTH))
from
PAYMENT P
WHERE
P.CREATED_DATE > (date_sub(CURDATE(), INTERVAL 1 MONTH));
This fails, as it's not the correct syntax for an AS alias. I've also tried this:
SET #s = monthname(date_sub(CURDATE(), INTERVAL 1 MONTH));
select
sum(P.AMOUNT) AS #s
from
PAYMENT P
WHERE
P.CREATED_DATE > (date_sub(CURDATE(), INTERVAL 1 MONTH));
Which also doesn't work...
Any ideas?
EDIT - With the above example i realize you could actually just use an additional column to return the date info. The problem is i have a few columns with various date ranges, like this:
select
sum(IF((P.CREATED_DATE BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 MONTH) and DATE_SUB(CURDATE(), INTERVAL 0 MONTH)),P.amount,0)) AS 'Month 0',
sum(IF((P.CREATED_DATE BETWEEN DATE_SUB(CURDATE(), INTERVAL 2 MONTH) and DATE_SUB(CURDATE(), INTERVAL 1 MONTH)),P.amount,0)) AS 'Month -1',
sum(IF((P.CREATED_DATE BETWEEN DATE_SUB(CURDATE(), INTERVAL 3 MONTH) and DATE_SUB(CURDATE(), INTERVAL 2 MONTH)),P.amount,0)) AS 'Month -2'
from
PAYMENT P
WHERE
P.AMOUNT < 100000;
So it's the "Month 0" etc i'm looking to replace with a function/query.
I need to select all rows in my database that were created last month.
For example, if the current month is January, then I want to return all rows that were created in December, if the month is February, then I want to return all rows that were created in January. I have a date_created column in my database that lists the date created in this format: 2007-06-05 14:50:17.
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Here's another alternative. Assuming you have an indexed DATE or DATETIME type field, this should use the index as the formatted dates will be type converted before the index is used. You should then see a range query rather than an index query when viewed with EXPLAIN.
SELECT
*
FROM
table
WHERE
date_created >= DATE_FORMAT( CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01' )
AND
date_created < DATE_FORMAT( CURRENT_DATE, '%Y/%m/01' )
If there are no future dates ...
SELECT *
FROM table_name
WHERE date_created > (NOW() - INTERVAL 1 MONTH);
Tested.
Alternatively to hobodave's answer
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
You could achieve the same with EXTRACT, using YEAR_MONTH as unit, thus you wouldn't need the AND, like so:
SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM date_created) = EXTRACT(YEAR_MONTH FROM CURDATE() - INTERVAL
1 MONTH)
SELECT *
FROM yourtable
where DATE_FORMAT(date_created, '%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 1 month),'%Y-%m')
This should return all the records from the previous calendar month, as opposed to the records for the last 30 or 31 days.
Even though the answer for this question has been selected already, however, I believe the simplest query will be
SELECT *
FROM table
WHERE
date_created BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND CURRENT_DATE();
WHERE created_date >= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY)
AND created_date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY)
This worked for me (Selects all records created from last month, regardless of the day you run the query this month)
Alternative with single condition
SELECT * FROM table
WHERE YEAR(date_created) * 12 + MONTH(date_created)
= YEAR(CURRENT_DATE) * 12 + MONTH(CURRENT_DATE) - 1
select fields FROM table
WHERE date_created LIKE concat(LEFT(DATE_SUB(NOW(), interval 1 month),7),'%');
this one will be able to take advantage of an index if your date_created is indexed, because it doesn't apply any transformation function to the field value.
Here is the query to get the records of the last month:
SELECT *
FROM `tablename`
WHERE `datefiled`
BETWEEN DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH )
AND
LAST_DAY( DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH ) )
Regards
- saqib
if you want to get orders from last month, you can try using
WHERE MONTH(order_date) = MONTH(CURRENT_DATE()) -1
One more way to do this in:
MYSQL
select * from <table_name> where date_created >= DATE_ADD(NOW(), INTERVAL -30 DAY);
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)