How can I fill gaps between rows? Im using table calendar but my query does not seem to be correct. The result is not showing the gaps..
SELECT calendar.datefield, t2.*
FROM store_relation t1,store t3, store_product t2
RIGHT JOIN calendar ON (DATE(t2.created) = calendar.datefield)
WHERE (calendar.datefield BETWEEN ('2012-07-15') and ('2012-07-20'))
AND t1.id_store_a = 1
AND t1.id_store_b = t2.id_store
AND t3.id = t2.id_store
AND t2.id_product = 11
ORDER By t2.price
Result:
datefield | id_product | id_store | created
2012-07-15 1 1 2012-07-15
2012-07-18 1 1 2012-07-18
2012-07-20 1 1 2012-07-20
2012-07-20 1 1 2012-07-20
I was waiting for the result
datefield | id_product | id_store | created
2012-07-15 1 1 2012-07-15
2012-07-16 null null null
2012-07-17 null null null
2012-07-18 1 1 2012-07-18
2012-07-19 null null null
2012-07-20 1 1 2012-07-20
2012-07-20 1 1 2012-07-20
When you have a LEFT/RIGHT JOIN and you are filtering on tables that are optionally included, the
WHEREclause will leave those records out. You need to include a condition that allows for the left part of theRIGHT JOINbeing missing:For example, if you look at your expected output, the value of
id_productis null, but you are filtering fort2.id_product = 11. So thenullrecords are never going to match11, and that’s why they are being left out.