我写的Oracle语法,你看看和MySQL有什么出入的地方改一改就可以了,应该差不多,我不知道你的A表的id和B表的id是否关联,如果不关联一个SQL语句实现不了,会出现冗余的笛卡尔积,
---A表和B表ID关联create or replace view view_name
as
select t1.id,
sum(t1.xx) x,
sum(t2.yy) y,
sum(t1.xx+t2.yy) z
from A t1,
B t2
where t1.id = t2.id
group by t1.id
先合并,再group相加,这是我的办法:mysql>select
*
from
c+------+------+|
id
|
num
|+------+------+|
a
|
10
||
b
|
20
|+------+------+2
rows
in
set
(0.00
sec)mysql>
select
*
from
d+------+------+|
id
|
num
|+------+------+|
a
|
20
||
b
|
30
|+------+------+2
rows
in
set
(0.00
sec)mysql>
mysql>
mysql>
select
id,sum(num)
from
(select
*
from
c
union
select
*
from
d)tablename
group
by
id+------+----------+|
id
|
sum(num)
|+------+----------+|
a
|
30
||
b
|
50
|+------+----------+2
rows
in
set
(0.00
sec)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)