MySQL Query - Records between Today and Last 30 Days
You need to apply DATE_FORMAT
in the SELECT
clause, not the WHERE
clause:
SELECT DATE_FORMAT(create_date, '%m/%d/%Y')
FROM mytable
WHERE create_date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
Also note that CURDATE()
returns only the DATE
portion of the date, so if you store create_date
as a DATETIME
with the time portion filled, this query will not select the today's records.
In this case, you'll need to use NOW
instead:
SELECT DATE_FORMAT(create_date, '%m/%d/%Y')
FROM mytable
WHERE create_date BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
Mysql -- Last 30 days
Edit: Changed query as per OP
select *
from cc_open_incident_view
WHERE date between (CURDATE() - INTERVAL 1 MONTH ) and CURDATE()
Previous Answer:
If date is saved as date
then use this
select *
from cc_open_incident_view
WHERE date >= (CURDATE() - INTERVAL 1 MONTH )
If date is saved as string then use this (assuming it is in dd/mm/yyyy ...
select *
from cc_open_incident_view
WHERE STR_TO_DATE(date ,''%d/%m/%y %h:%i:%s')>= (CURDATE() - INTERVAL 1 MONTH )
how to select last 30 days mysql record and group by each day
Save your date as Y-m-d and run the SQL below.
SELECT * FROM your_table WHERE token = 'vitC' AND dateview BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND NOW() GROUP BY dateview
How to get records from last Month (not last 30 days) in mysql query
This is the query of getting all rows in a table belonging to last month in mysql:
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
An alternative is:
SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM date_created) = EXTRACT(YEAR_MONTH FROM CURDATE() - INTERVAL
1 MONTH)
How to select last 30 days dates in MySQL?
I hacked this together from someone else's code, but it seems to work:
SELECT DATE_FORMAT(m1, '%d %b %Y')
FROM (
SELECT SUBDATE( NOW() , INTERVAL 30 DAY) + INTERVAL m DAY AS m1
FROM (
select @rownum:=@rownum+1 as m from
(select 1 union select 2 union select 3 union select 4) t1,
(select 1 union select 2 union select 3 union select 4) t2,
(select 1 union select 2 union select 3 union select 4) t3,
(select 1 union select 2 union select 3 union select 4) t4,
(select @rownum:=-1) t0
) d1
) d2
WHERE m1 <= now()
ORDER BY m1
The original code by valex is here:
How to get a list of months between two dates in mysql
Related Topics
Error:More Than One Row Returned by a Subquery Used as an Expression
Alternative to Row_Number() to Get Row Position
Select Ids from Multiple Rows Where Column Values Satisfy One Condition But Not Another
How to Split One Column into Two Columns in SQL Server
1052: Column 'Id' in Field List Is Ambiguous
Duplicate Rows When Joining Tables
Sqlstate[01000]: Warning: 1265 Data Truncated for Column
How to Select Multiple Values in One Field MySQL
Want to Run Multiple SQL Script File in One Go With in Sqlplus
How to Get Only Digits from String in MySQL
How to Import CSV Data into a Table Without Knowing the Columns of the Csv
Error 1265. Data Truncated for Column When Trying to Load Data from Txt File
Using T-Sql, Return Nth Delimited Element from a String
Could Not Load File or Assembly Error in .Net Standard 2.0 Class Library
Find All Parent Records Where All Child Records Have a Given Value (But Not Just Some Child Records)
How to Use Return Value of Insert...Returning in Another Insert