php - MySQL query and subquery -


i have query this:

select sum(price) ginto, (    (select count(price)*9.99 sms_logs price = '200000')+    (select count(price)*3.99 sms_logs price = '60000')+    (select count(price)*1.99 sms_logs price = '24000')+    (select count(price)*0.99 sms_logs price = '11000') ) usd, date_format(date,'%y-%m-%d') date sms_logs  date >='2015-03-20' , date <= '2015-04-30' group date_format(date,'%y-%m-%d') 

i want show data as:

 ginto  ||   usd  ||   date 2222000 ||   200  || 2015-03-23 3366000 ||   300  || 2015-03-24 11000   ||   10   || 2015-03-25 

but result query:

 ginto  ||   usd ||   date 2222000 || 284.65|| 2015-03-23 3366000 || 284.65|| 2015-03-24 11000   || 284.65|| 2015-03-25 

with usd column shows summary of records. why?

your count calls run in separate queries, no group by clause, applied on entire table, regardless of "main" query's goruping. 1 way solve rid of subqueries , bring them "main" query:

select   sum(price) ginto,          sum(case price when '200000' 9.99                         when '60000'  3.99                         when '24000'  1.99                         when '11000'  0.99               end) usd,          date_format(date, '%y-%m-%d') date      sms_logs     date >='2015-03-20' , date <= '2015-04-30' group date_format(date,'%y-%m-%d') 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -