This is my MySQL query:
SELECT ROUND( AVG(p.votes), 2) AS 'votes', games.*
FROM games
LEFT JOIN polls p ON (p.gid = games.id)
GROUP BY games.id;
It is working fine, but if there aren’t any votes on game in the polls table – the votes field is NULL then in the result – how can I replace it so it will show 0 instead (as there are 0 rows found in the polls).
Also:
Is it possible to name my condition and then use it fe. for calculations in MySQL?
This is what I mean:
SELECT ROUND( AVG(votes), 2) AS 'votes', (votes / 50 * 100) FROM `polls`;
Will throw up an error message saying that votes is a unknown column there.
So is it possible to use the condition (as ‘votes‘) name for calculating purposes in my query?
First:
Second :
no, not possible in the exact same statement. It’s possible if the aliased part is in a subquery.
So you have to rewrite
COALESCE and IFNULL achieve the same goal (maybe not exactly the same way), but COALESCE is ANSI SQL.