2016-04-25 128 views
1

如何汇总下面两个查询的结果?添加两个mysql查询的结果

select firstname, surname, COUNT(*) as Built 
from  orders 
     join users on orders.builder = users.id 
where bStop > 1461496211 and bStop < 1461582649 
group by users.id; 


select firstname, surname, COUNT(*) as Built 
from production_points 
     join users on production_points.rewarded = users.id 
where Date(datetime) = '2016-04-25' 
group by users.id 

相同的用户可以在两个表中,所以我要总结他的研究结果,不想两条独立的线路,即显示4和第二个6就总10

回答

0

如建议通过你们的研究后,这里是解决方案:

select uid, firstname, surname, Count(*) as Built from (
select users.id as uid, firstname, surname from orders join users on orders.builder = users.id where bStop > 1461542400 and bStop < 1461592622 
union all 
select users.id as uid, firstname, surname from production_points join users on production_points.rewarded = users.id where Date(datetime) ='2016-04-25' 
) performance group by uid; 
1

你可能会得到每个的结果,并将它们分配给不同的变量。

并总结变量。