如果两表左连接查询,如果右表没有相关内容,则在查询结果中完全不展示。例如统计数量结果,并不是以0为结果展示,而是完全不展示,容易造成遗漏。
例如
SELECT a.fund_code, t1.tot from gmxp_check as a LEFT JOIN (SELECT fund_code, count(distinct(file_type)) as tot from gmxp_file GROUP BY fund_code) t1 on t1.fund_code=a.fund_code;
这样如果t1.tot如果为0,就会在结果中不显示。不方便查询。
SELECT a.fund_code, IFNULL(t1.tot, 0) from gmxp_check as a LEFT JOIN (SELECT fund_code, count(distinct(file_type)) as tot from gmxp_file GROUP BY fund_code) t1 on t1.fund_code=a.fund_code;
在IFNULL(t1.tot, 0)作用下,显示为
fund_code 0