简体   繁体   中英

how to combine sql statement to get it in one

SELECT mem_id, sum(commission)
FROM `member_commission`
group by mem_id

第一个sql命令导致

SELECT mem_id, sum(crowdfund)
FROM `crowdfund`
group by mem_id

第一个sql命令导致

i want to combine both of these commands in one and then group them by mem_id

+

both of my table (member_commission, crowd fund) have a (datetime stamp - which records date and time) column which is time column present in membecommission and distrbutedmonth in crowdfund.... and i just want the result of the present month only...

the datetime column records date and time in this format : 2018-08-22 22:02:09

One method uses union all and group by :

select mem_id, sum(commission), sum(crowdfund)
from ((select mc.mem_id, mc.commission, 0 as crowdfund
       from member_commission mc
      ) union all
      (select cf.mem_id, 0, cf.crowdfund
       from crowdfund cf
      )
     ) x
group by mem_id;

This is tricky because members could be in either table -- and presumably you still want them in the result set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM