views:

382

answers:

2

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?

+2  A: 

select * from *table_name* where *datetime_column* between '01/01/2009' and curdate()

or

select * from *table_name* where *datetime_column* >= '01/01/2009' and *datetime_column* <= curdate()

Noah Heldman
Cool. It works. LAST question: If I want to show data per day since 01/01/2009, must use count function right? Any idea how to implement?
xiasue1982
You can use "group by" and "order by", like this:select * from [table_name] where [datetime_column] between '01/01/2009' and curdate() group by [datetime_column] order by [datetime_column]
Noah Heldman
i tried this: SELECT *, count(*) from table WHERE datetime BETWEEN '2009-01-01' AND '2009-09-01' GROUP BY datetime;but the count is 1. it supposed to be, 10, 5, 8 etc...
xiasue1982
You have to include the column name you want the count of inside count(). So, you would use count(datetime) in this case.
Noah Heldman
Also, you have to make sure that you are grouping on fields with the same value. If you have want to group all values for a date regardless of time, you will have to use the DATE() function to get the date part of the datetime.
Noah Heldman
You will want to group by Day(<datetime_column>) not just the column because the datetime field goes down to ms I believe which means you will end up with the results grouped at ms instead of day.
CSharpAtl
"SELECT * , count(datetime) FROM table WHERE datetime BETWEEN '2009-01-01' AND CURDATE() GROUP BY Day(datetime)";It works.. COOL. My query correct right? Just want to double check?
xiasue1982
WAIT. @CSharpAtl: If i group by day, it end up showing 31 days. E.g my date between 1-jan-09 and CURDATE(), but it shows JANuary data only? Why? What should I group?????????????
xiasue1982
Problem solved: I group by DATE(datetime). Thanks in millions guys :)
xiasue1982
A: 

You can use now() like:

Select data from tablename where datetime >= "01-01-2009 00:00:00" and datetime <= now();

Kerri