用全連接吧,full join Select a.*,b.* From a full join b on ........
解决方案 »
- 如何删除没有子记录的主表记录...................
- Mysql 中的declare
- 一段汇总 sql 求助---急啊,现场等待回复!!
- 请问这样的更新语句该怎么写?
- 求SQL正确的安装方法
- 如何檢查哪些表的索引需要重建或重組?
- SQLServer中的Invalid Descriptor Index问题
- 又一问题。
- 请用过Sprinta2000.jar链接SQL SERVER2000数据库的大侠帮小弟看看下面的链接出错在哪里?另外请说说怎么配置Sprinta2000.jar。谢谢!!
- 如何取出某一用户的密码,再原封不动的改回去?
- 请教,表a数据量太大,而我平时只查询5天内的数据,请问怎么提高查询效率?
- 问个关于考勤表的问题
UNION ALL
SELECT 编码,年度,月,当月,累计,上月,0 支出, 收入 FROM 表2
from 表1 a
INNER JOIN 表2 b
ON a.编码 = b.编码 AND a.年度 = b.年度 AND a.月 = b.月
if object_id('[ta]') is not null drop table [ta]
go
create table [ta]([编码] int,[年度] int,[月] varchar(2),[当月] int,[累计] int,[上月] int,[收入] int)
insert [ta]
select 196207,2008,'01',514,1028,0,0 union all
select 196308,2008,'01',424,848,0,0 union all
select 196607,2008,'01',435,870,0,0 union all
select 196608,2008,'01',396,792,0,0
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([编码] int,[年度] int,[月] varchar(2),[当月] int,[累计] int,[上月] int,[支出] int)
insert [tb]
select 196207,2008,'01',514,1028,0,0 union all
select 196308,2008,'01',424,848,0,0 union all
select 196607,2008,'01',435,870,0,0 union all
select 196608,2008,'01',396,792,0,0
---查询---
select 编码,年度,月,当月,累计,上月,0 as 支出, 收入 from ta
union all
select 编码,年度,月,当月,累计,上月,支出,0 as 收入 from tb---结果---
编码 年度 月 当月 累计 上月 支出 收入
----------- ----------- ---- ----------- ----------- ----------- ----------- -----------
196207 2008 01 514 1028 0 0 0
196308 2008 01 424 848 0 0 0
196607 2008 01 435 870 0 0 0
196608 2008 01 396 792 0 0 0
196207 2008 01 514 1028 0 0 0
196308 2008 01 424 848 0 0 0
196607 2008 01 435 870 0 0 0
196608 2008 01 396 792 0 0 0(所影响的行数为 8 行)