I want to compare the current date with a date from a database table.
This code:
DATE_FORMAT(dt,'%y-%m')
gives me a date like this 2020-10
and i want that this code:
DATE_ADD(CURRENT_DATE, INTERVAL - 1 month)
gets date like 2020-10 not 2020-10-12(some day)
Here is the whole sql query:
select count(*)
from app_tickets t
where t.status= 3
and DATE_FORMAT(dt,'%y-%m')=DATE_ADD(CURRENT_DATE, INTERVAL - 1 month)
Thanks for help:)
This can make use of indexes
select count(*)
from app_tickets t
where t.status = 3
and t.dt >= DATE_FORMAT(CURRENT_DATE - interval 1 month ,'%Y-%m-01')
and t.dt < DATE_FORMAT(CURRENT_DATE ,'%Y-%m-01')
I am trying to do a single query over a period of a month. This is a working query:
SELECT AVG(days)
FROM (SELECT datediff(IF(MIN(date_end) = '0000-00-00', DATE(NOW()), MAX(date_end)), MIN(date_start)) AS days
FROM tenancies
WHERE deleted_at IS NULL AND date_start < DATE_SUB(NOW(), INTERVAL 1 MONTH)
GROUP BY tenancies.tenant_id)
I want to replace NOW() with a date.
I have another query:
SELECT calendar_date
FROM calendar_dates
WHERE calendar_date BETWEEN NOW() - INTERVAL 1 MONTH AND NOW()
This gets me all the dates I want. If I try to do a double-subquery it doesn't recognize the calendar_date:
SELECT calendar_date, (SELECT AVG(days)
FROM (SELECT datediff(IF(MIN(date_end) = '0000-00-00', DATE(calendar_date), MAX(date_end)), MIN(date_start)) AS days
FROM tenancies
WHERE deleted_at IS NULL AND date_start < DATE_SUB(calendar_date, INTERVAL 1 MONTH)
GROUP BY tenancies.tenant_id) d) AS days
FROM calendar_dates
WHERE calendar_date BETWEEN NOW() - INTERVAL 1 MONTH AND NOW()
Anyone have any suggestions?
I assume it does not recognize DATE(calendar_date) and DATE_SUB(calendar_date, INTERVAL 1 MONTH), If right, Your tenancies table should have a calendar_date field in itself, otherwise you need to join tenancies with calendar_dates table in the middle query too. Because thats a separate query.
I need to make statistics page for billing with auto query. For example now is july and query must show count of record for the june and current count of records on current day(only july). Sort of:
"records for the last month - 85, for the current day - 32"
I have table customer and row create_time but it is in unix timestamp. Tried
SELECT COUNT(*) FROM customer WHERE create_time >=
UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 1 MONTH))
but its absolutely not what i want.
I would appreciate for any help.
Try this:
SELECT *
FROM ( SELECT COUNT(*) AS total_previous_month
FROM customer
WHERE create_time >= concat(date_format(LAST_DAY(now() - interval 1 month),'%Y-%m-'),'01')
AND create_time < LAST_DAY(now() - interval 1 month )
) AS s1,
( SELECT COUNT(*) AS total_previous_month
FROM customer
WHERE create_time >= concat(extract(year from now()),'-',extract(month from now()),'-01')
AND create_time <= now()
) AS s2
You can do this by adding condition to the WHERE:
create_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 month))
in PHP you can calculate the one month back date
$newDate = strtotime('-1 month');
and use this in query
I want to select all dates that are between the current date and 3 months before.
I tried using this query but it isn't working right.
$sql = mysql_query("
SELECT *
FROM date
WHERE d_date BETWEEN NOW() AND NOW() - INTERVAL 3 MONTH
");
Please if you could help me write the right syntax.
You need to swap your bounaries, and it will work:
SELECT * FROM date
WHERE d_date BETWEEN now() - INTERVAL 3 MONTH AND now()
For example, this query returns true (SQLFiddle):
SELECT (now() - interval 1 month)
BETWEEN now() - interval 3 month AND now()
SELECT * FROM Table
WHERE anydate_col BETWEEN NOW() AND DATE_ADD( NOW() , INTERVAL +3 MONTH)
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)