我有以下查询:
$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";
它基本上是从不同表中检索到的2个SUM
个值。
在其中一个SELECT
语句中,该记录尚未出现在数据库中并创建NULL
,即使第二个SELECT
获取值,也会创建一个NULL
1}}。
问题:如果记录尚未存在于数据库中,如何将SELECT
语句的结果变为0。
答案 0 :(得分:1)
使用IFNULL()
:
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
答案 1 :(得分:0)
试试这个
$ query =“SELECT(SELECT SUM(net_amount IS NOT NULL然后net_amount else 0 end的情况)FROM fin_costos WHERE month ='1'AND group_of_costos ='general'AND year_analysis ='2014')+ (SELECT SUM(net_amount IS NOT NULL然后net_amount else 0 end的情况)FROM em2_fin_costs WHERE month ='1'AND group_of_costos ='general'AND year_analysis ='2014')AS total“;