子維 宋
子維 宋

Reputation: 85

Combine 2 different mySql result into one table

I like to combine two different mysql results into one table

first sql is

select     sum(cnt)             
from  xx_dept_hardware_cnt_v            

and result will be

sum(cnt)
---------
67

second sql is

select sum(cnt)             
from xx_dept_emp_cnt_v          
where org_cb_id in (select cb_id from xx_dept_hardware_cnt_v)   

result will be

sum(cnt)
---------
62

how to write sql that will give me the result?

sum(cnt)        sum(cnt)
---------       ---------
67              62

Upvotes: 0

Views: 60

Answers (2)

mehere
mehere

Reputation: 1556

select sum(cnt), (select sum(s.cnt) from xx_dept_hardware_cnt_v s where s.org_cb_id in (select x.cb_id from xx_dept_hardware_cnt_v x) ) from xx_dept_hardware_cnt_v

You can use subqueries in select.

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1271151

You can put the two subqueries in the select. I prefer to put them in the from clause and use cross join:

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

Upvotes: 2

Related Questions