I have a mysql table with data connected to dates. Each row has data and a date, like this:
2009-06-25 75
2009-07-01 100
2009-07-02 120
I have a mysql query that select all data between two dates. This is the query:
SELECT data FROM tbl WHERE date BETWEEN date1 AND date2
My problem is that I also need to get the rows between date1 and date2 even if there is no data for a day.
So my query would miss the dates that are empty between 2009-06-25 and 2009-07-01.
Can I in some way add these dates with just 0 as data?
IF YOU CAN AVOID IT.. DON'T DO IT
Databases aren't really designed for this, you are effectively trying to create data (albeit a list of dates) within a query.
For anyone who has an application layer above the DB query the simplest solution is to fill in the blank data there.
You'll more than likely be looping through the query results anyway and can implement something like this:
The benefits of this are reduced data transmission; simpler, easier to debug queries; and no worry of over-flowing the calendar table.
Do you have a table that has all dates? If not, you might want to consider implementing a calendar table and left joining your data onto the calendar table.
You can use a concept that is frequently referred to as 'calendar tables'. Here is a good guide on how to create calendar tables in MySql:
You might want to create table
cal
instead of the subselect.you must add 1 day to the end date, using:
DATE_ADD('$end_date', INTERVAL 1 DAY)
But this query not show proper data.
Eg
But it's show data
its very easy to handle this situation
You can use BETWEEN CLAUSE in combination with date_sub( now( ) , INTERVAL 30 DAY ) AND NOW( )
Happy Coding :)