一种将两个mysql查询合并为一个查询的方法

时间:2014-12-03 05:55:42

标签: mysql

有没有办法合并这两个mysql命令只是为了得到一个mysql命令?

这是我的第一个sql

select cnt1, cnt2

from (select sum(cnt) as cnt1           
  from xx_dept_hardware_cnt_v  
 ) h cross join
 (select sum(cnt) as cnt2           
  from xx_dept_emp_cnt_v          
  where org_cb_id in (select cb_id from xx_dept_hardware_cnt_v) 
 ) d  

结果

cnt1     cnt2
-----    ----
67       62

第二个mysql命令

select     s.name software_name,
          count(s.name) install_qty

from  (select name,version,hardware_id 
   from softwares 
   group by name,version,hardware_id) s ,
   dico_soft ds

where  s.name = ds.extracted 
        and ds.formatted ='OS'
group by s.name

结果是

software_name          install_qty
------------           -------------
Microsoft Window 7      1
Microsoft Window 8      42
Microsoft Window 8.1    2

我试图获得

software_name           cnt1   cnt2       install_qty
------------            -----  -----      -------------
Microsoft Window 7      67     62          1
Microsoft Window 8      67     62          42
Microsoft Window 8.1    67     62          2

1 个答案:

答案 0 :(得分:0)

试试这个:

 select cnt1, cnt2

from (select sum(cnt) as cnt1           
  from xx_dept_hardware_cnt_v  
 ) h cross join
 (select sum(cnt) as cnt2           
  from xx_dept_emp_cnt_v          
  where org_cb_id in (select cb_id from xx_dept_hardware_cnt_v) 
 ) d 
union
select     s.name software_name,
          count(s.name) install_qty

from  (select name,version,hardware_id 
   from softwares 
   group by name,version,hardware_id) s ,
   dico_soft ds

where  s.name = ds.extracted 
        and ds.formatted ='OS'
group by s.name

如果你想要重复的值而不是使用union all。