PHP/MYSQL:如果记录尚未在数据库中,则值为0

PHP/MYSQL:如果记录尚未在数据库中,则值为0,php,mysql,mysqli,Php,Mysql,Mysqli,我有以下疑问: $query = "SELECT (SELECT SUM(net_amount) FROM fin_costos WHERE month='1' AND group_of_costos='general' AND year_analysis='2014' ) + (SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='1' AND group_of_costos='general' AND year_analysis='2

我有以下疑问:

$query = "SELECT (SELECT SUM(net_amount) FROM fin_costos WHERE month='1' AND group_of_costos='general' AND year_analysis='2014' ) + 
(SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='1' AND group_of_costos='general' AND year_analysis='2014') AS total";
它基本上是从不同表中检索到的两个值的总和。 当在其中一个SELECT语句中,记录尚未在数据库中并创建空值时,就会出现问题,即使第二个SELECT语句获得值,也会创建空值

问题:如果记录尚未在数据库中,如何将SELECT语句的结果转换为0。

使用IFNULL:

试试这个

$query=选择SELECT SUMcase当净金额不为空时,则净金额否则0从fin\u costos结束,其中月份为'1',集团为'u costos='general',年份为'2014'+
当净金额不为空时,选择SUMcase,然后从em2财务成本中选择净金额0,其中月份为'1',成本组为'general',年度分析为'2014'

谢谢。第一次选择时,我总是得到空值。
SELECT IFNULL((SELECT SUM(net_amount) FROM fin_costos WHERE month='1' AND group_of_costos='general' AND year_analysis='2014' ), 0) + 
IFNULL((SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='1' AND group_of_costos='general' AND year_analysis='2014'), 0) AS total