mysql - adding two subqueries to produce third column -
i need add 2 values 2 subqueries , add third column. can write whole subqueries twice produce sum, there better way it?
select d.id, concat(d.disease, '( ', d.disease_nepali, ' ) ') disease, ifnull((select patients.d_o_m + patients.d_o_f patients clinic = 22 , patients.disease = p.disease), 0) 'district1', ifnull((select patients.d_o_m + patients.d_o_f patients clinic = 21 , disease = p.disease), 0) 'district2' diseases d left join patients p on (d.id = p.disease , p.district = 9 , p.status = 1 , p.report_date '2014-03%') group disease
you can use sub select
select t.*,t.district1 + t.district2 `new_col` ( select d.id, concat(d.disease, '( ' ,d.disease_nepali, ' ) ') disease, ifnull((select patients.d_o_m+patients.d_o_f patients clinic = 22 , patients.disease = p.disease),0) `district1` , ifnull((select patients.d_o_m+patients.d_o_f patients clinic = 21 , disease = p.disease),0) `district2` diseases d left join patients p on (d.id = p.disease , p.district = 9 , p.status = 1 , p.report_date '2014-03%') group disease ) t
Comments
Post a Comment