I have connected three tables to each other.
I’m querying keywords + statistics.
I need to get data out by date range, and
if there is no data for that specific date range
i would like it to return 0 statistics
Lets say my query is:
SELECT keyword,SUM(stat) FROM keywords WHERE date >=’2012-07-10′ GROUP BY keyword;
It would return
|my keyword 1|3|
|my keyword 2|6|
Example table content:
| id | keyword | stat | date | keywordGroup |
| 1 | my keyword 1 | 2 | 2012-07-11 | 1 |
| 2 | my keyword 1 | 1 | 2012-07-12 | 1 |
| 3 | my keyword 2 | 6 | 2012-07-11 | 1 |
| 4 | my keyword 3 | 10 | 2012-07-09 | 1 |
But there are some keywords which have no stats for that date range
but I would still like to get those keywords showing 0 as stats.
Like this:
|my keyword 1|3|
|my keyword 2|6|
|my keyword 3|0|
The problem is that the where clause blocks values that are not found, is there a workaround.
As I said I have three tables with relations, I use LEFT JOIN to
query data, for simplification I left that part out of my example query.
Table: Campaigns
id
name
Table: KeywordGroup
id
name
campaign
Table: KeywordData
id
keyword
stat
date
keywordGroup
Assuming you are joing tables with left joins correctly, use:
If it is WHERE clause that filters out some stats before even grouping is applied, then you need to move the date condition into LEFT JOIN condition, ie.
But I cannot help further without seeing actual queries. 🙂