Reputation:
How to select data from mysql table past date to current date? For example, Select data from 1 january 2009 until current date ??
My column "datetime" is in datetime date type. Please help, thanks
Edit:
If let say i want to get day per day data from 1 january 2009, how to write the query? Use count and between function?
Upvotes: 57
Views: 263262
Reputation: 98871
Late answer, but the accepted answer didn't work for me.
If you set both start and end dates manually (not using curdate()
), make sure to specify the hours, minutes and seconds (2019-12-02 23:59:59
) on the end date or you won't get any results from that day, i.e.:
This WILL include records from 2019-12-02
:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02 23:59:59'
This WON'T include records from 2019-12-02
:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02'
Upvotes: 4
Reputation: 1366
All the above works, and here is another way if you just want to number of days/time back rather a entering date
select * from *table_name* where *datetime_column* BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
Upvotes: 26
Reputation: 6874
select * from *table_name* where *datetime_column* between '01/01/2009' and curdate()
or using >=
and <=
:
select * from *table_name* where *datetime_column* >= '01/01/2009' and *datetime_column* <= curdate()
Upvotes: 93
Reputation: 163
You can use now()
like:
Select data from tablename where datetime >= "01-01-2009 00:00:00" and datetime <= now();
Upvotes: 15