HI all,
I have these two statement: $sql1 = "select sum(to_pay) as p_pay from tbl_app where set_payment=0";
$sql2 = "select sum(set_payment) as s_pay from tbl_app where set_payment>0";
is there away to combine the two?
thanks!
Not really. You can run it as subqueries, but it would be no real benefit. On the contrary, it would just mess your code up.
You could. You'd have to use a case though...
SELECT SUM(CASE WHEN `set_payment` = 0 THEN `to_pay` ELSE 0 END) AS `p_pay`, SUM(CASE WHEN `set_payment` > 0 THEN `set_payment` ELSE 0 END) AS `s_pay` FROM `tbl_app`
That should do it. Simple. concise.
Nice query bpat, I didn't think of that.
Do you have any idea how that query will perform compared to having two queries?
I'm not sure. It shouldn't take much longer than a regular select SUM() query; however, I haven't done much benchmarking on the matter.