mysql - JOIN SQL queries results in one -
i make query of several select in one, query works without last select have error operand should contain 1 column
can tell me how correct ??
select ( select count(*) mytable type = 'mss' , year(date) = year(curdate()) , month(date) = month(curdate()) ) total_mss, ( select count(*) mytable type = 'mss' , s = 0 , year(date) = year(curdate()) , month(date) = month(curdate()) ) mss_s, ( select count(*) mytable type = 'mss' , s != 0 , year(date) = year(curdate()) , month(date) = month(curdate()) ) mss_f, ( select count(*) nb ,or top mytable type = 'mss' , year(date) = year(curdate()) , month(date) = month(curdate()) group or order nb desc limit 1 )
if execute them separately
total_mss mss_s mss_f 99 12 87
nb top 78 j.r@domain.com
first of should use union
join results , second or
, top
keywords need escape them (assuming table has column named or) :
select count(*) nb , `or` `top` mytable type = 'mss' , year(date) = year(curdate()) , month(date) = month(curdate()) group `or` order nb desc limit 1
also queries in union must have same amount of fields , have first 1 field , second 2 fields
select sum(if(`top` = 'one',`nb`,0)) first_one, sum(if(`top` = 'two',`nb`,0)) second_one, sum(if(`top` = 'three',`nb`,0)) thrid_one, sum(if(`top` not in ('three','two','one'),`nb`,0)) forth_one, group_concat(if(`top` not in ('three','two','one'),`top`,'') separator '') `or` ( select count(*) nb, 'one' `top` mytable type = 'mss' , year(date) = year(curdate()) , month(date) = month(curdate()) union select count(*) nb, 'two' `top` mytable type = 'mss' , s=0 , year(date) = year(curdate()) , month(date) = month(curdate()) union select count(*) nb, 'three' `top` mytable type = 'mss' , s<>0 , year(date) = year(curdate()) , month(date) = month(curdate()) union select `nb`, `top` from( select count(*) nb , `or` `top` mytable type = 'mss' , year(date) = year(curdate()) , month(date) = month(curdate()) group `or` order nb desc limit 1 ) tmp )as tmp1
update first 3 queries can resumed :
select sum(if(s=0,1,0)) mss_s, sum(if(s<>0,1,0)) mss_f, count(*) total_mss mytable type = 'mss' , year(date) = year(curdate()) , month(date) = month(curdate())
Comments
Post a Comment