Mega Code Archive

 
Categories / MySQL / Function
 

IF( ) and IFNULL( ) are useful for catching divide-by-zero operations

mysql> mysql> mysql> SET @hits = 0, @atbats = 0; Query OK, 0 rows affected (0.00 sec) mysql> SELECT @hits, @atbats, @hits/@atbats AS 'batting average'; +-------+---------+-----------------+ | @hits | @atbats | batting average | +-------+---------+-----------------+ |     0 |       0 |            NULL | +-------+---------+-----------------+ 1 row in set (0.00 sec) mysql>