I have this simple MySQL statement:
SELECT
((AVG(q1) + AVG(q8) + AVG(q15)) / 3 ) AS Res
FROM tresults
WHERE id = '1' AND date = 'MARCH2010' AND q25 = '1'
GROUP BY q25
Now, if there are no rows with the date MARCH2010 then the query returns zero results (which is correct) but I’d like it to return a row – even if the result is NULL.
You can just select a single row as a constant, and then
left joinit to your result set:How this works:
select "your constant" as constantalways returns a single rowleft joinalways returns all of the rows in thelefttable at least oncerighttable has no rows, then the entirelefttable is extended with a bunch of null columns, and the result has one rowrighttable has n rows, the result has n rows that each have an additional “your constant” column