I have the following code:
SELECT q25, (
(
AVG( q1 ) + AVG( q2 ) + AVG( q3 ) ) /3 ) AS Overall
FROM t_results
WHERE brand = 'XYZ'
AND DATE = 'MAY2012'
GROUP BY q25
ORDER BY Overall
DESC LIMIT 1
If there is no data found by the query phpmyadmin returns the following message (which is quite correct):
MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0178 sec )
However, what I’d like is to actually return a NULL value, is this possible? I appreciate this might not be best practise but I’m working with inherited code and this might be the simplist and quickest route to a solution.
Thanks as always,
H.
Create a table with exactly one row. Then you can use left join to achieve the desired
NULLresult.You can also replace the dummy table with a subquery:
Tested this via sqlfiddle, where you can also experiment with alternatives.
The conditions selecting the result, which used to be in the
WHEREclause, now have to go into theONclause. Otherwise the left join would produce non-NULLrows which would be removed by theWHERE, instead of generating a singleNULLrow if no matching row could be found. If there were noWHEREconditions in the original query,ON 1could be used to expressany row matches.