请大家帮我写一条sql语句,
q_name q_qunianbf
王五3434
赵钱1212
赵钱1212
张震20000
赵钱55555
王麻1666
这是表1
rlzy_name baofei_baofei
赵钱100
赵钱100
赵钱200
赵钱400
张震100
张震0
张震0
王五1111
张震500
赵钱1212
这个表2
我想这两个表合并。每个表人名同样的相加。之后列在一个表里。这个sql语句怎么写呀
------解决方案--------------------CREATE TABLE A(q_name NCHAR(10),q_qunianbf INT)
INSERT A SELECT '王五 ',3434
INSERT A SELECT '赵钱 ',1212
INSERT A SELECT '赵钱 ',1212
INSERT A SELECT '张震 ',20000
INSERT A SELECT '赵钱 ',55555
INSERT A SELECT '王麻 ',1666
--SELECT * FROM A
CREATE TABLE B(rlzy_name NCHAR(10),baofei_baofei INT)
INSERT B SELECT '赵钱 ',100
INSERT B SELECT '赵钱 ',100
INSERT B SELECT '赵钱 ',200
INSERT B SELECT '赵钱 ',400
INSERT B SELECT '张震 ',100
INSERT B SELECT '张震 ',0
INSERT B SELECT '张震 ',0
INSERT B SELECT '王五 ',1111
INSERT B SELECT '张震 ',500
INSERT B SELECT '赵钱 ',1212
--SELECT * FROM B
SELECT
NAME,
Q_QUNIANBF=SUM(Q_QUNIANBF),
BAOFEI_BAOFEI=SUM(BAOFEI_BAOFEI)
FROM
(
SELECT Q_NAME AS NAME, Q_QUNIANBF, 0 AS BAOFEI_BAOFEI FROM A
UNION ALL
SELECT RLZY_NAME AS NAME,0 AS Q_QUNIANBF,BAOFEI_BAOFEI FROM B
)I
GROUP BY NAME
DROP TABLE A,B