我喜欢将两个不同的mysql结果合并到一个表中
第一个sql是
select sum(cnt)
from xx_dept_hardware_cnt_v
,结果将是
sum(cnt)
---------
67
第二个sql是
select sum(cnt)
from xx_dept_emp_cnt_v
where org_cb_id in (select cb_id from xx_dept_hardware_cnt_v)
结果将是
sum(cnt)
---------
62
如何编写能给我结果的sql?
sum(cnt) sum(cnt)
--------- ---------
67 62
答案 0 :(得分:2)
您可以将两个子查询放在select
中。我更喜欢将它们放在from
子句中并使用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
答案 1 :(得分:0)
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
您可以在select中使用子查询。