问题:
两表求并集问题假设有两表A,B
create table A(
a int(5) primary key,
b int(5) not null
);create table B(
a int(5) primary key,
c int(5) not null
);create table C(
a int(5) primary key,
d int(5) not null
);
要求是得到表C, A.a = B.a时, C.d 的值为A.b和B.c相加,否则即为A.b 或者B.c
例如
A
(1,10)
(2,11)
(7,12) B
(1,22)
(2,23)
(3,24)
(4,25)
(5,26)
(6,27)
(7,28)
那么C为
C
(1,32)
(2,34)
(3,24)
(4,25)
(5,26)
(6,27)
(7,40)注意:也可能A的元素比B多,谢谢!
两表求并集问题假设有两表A,B
create table A(
a int(5) primary key,
b int(5) not null
);create table B(
a int(5) primary key,
c int(5) not null
);create table C(
a int(5) primary key,
d int(5) not null
);
要求是得到表C, A.a = B.a时, C.d 的值为A.b和B.c相加,否则即为A.b 或者B.c
例如
A
(1,10)
(2,11)
(7,12) B
(1,22)
(2,23)
(3,24)
(4,25)
(5,26)
(6,27)
(7,28)
那么C为
C
(1,32)
(2,34)
(3,24)
(4,25)
(5,26)
(6,27)
(7,40)注意:也可能A的元素比B多,谢谢!
+---+----+
| a | b |
+---+----+
| 1 | 10 |
| 2 | 11 |
| 7 | 12 |
+---+----+
3 rows in set (0.00 sec)mysql> select * from b;
+---+----+
| a | c |
+---+----+
| 1 | 22 |
| 2 | 23 |
| 3 | 24 |
| 4 | 25 |
| 5 | 26 |
| 6 | 27 |
| 7 | 28 |
+---+----+
7 rows in set (0.00 sec)mysql> select a,sum(b) from
-> (
-> select a,b from a
-> union all
-> select a,c from b
-> ) t
-> group by a;
+---+--------+
| a | sum(b) |
+---+--------+
| 1 | 32 |
| 2 | 34 |
| 3 | 24 |
| 4 | 25 |
| 5 | 26 |
| 6 | 27 |
| 7 | 40 |
+---+--------+
7 rows in set (0.13 sec)mysql>
'截至2010-10-02 18:21:16 用户结帖率0.00% 当您的问题得到解答后请及时结贴.
http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html8、如何给分和结贴?
http://community.csdn.net/Help/HelpCenter.htm#结帖