新手,急求教如下语句
表1
id 字段1
1 aa
3 bb
表2
id 字段2
2 cc
4 dd
语句结果得出:
id 字段1 字段2
1 aa Null
2 Null cc
3 bb Null
4 Null dd
得出结果的语句怎么写啊?
表1
id 字段1
1 aa
3 bb
表2
id 字段2
2 cc
4 dd
语句结果得出:
id 字段1 字段2
1 aa Null
2 Null cc
3 bb Null
4 Null dd
得出结果的语句怎么写啊?
left join 表2 on 表1.id=表2.id
union
select 表2.id,字段1,字段2 From 表1
right join 表2 on 表1.id=表2.id
Select
IsNull(A.id, B.id) As id,
A.字段1,
B.字段2
From
表1 A
Full Join
表2 B
On A.id = B.id
where a.*=b.*
insert into a select 1,'aa'
insert into a select 3,'bb'create table b (id int,name varchar(10))
insert into b select 2,'cc'
insert into b select 4,'dd'
--方法一:
select isnull(a.id,b.id) as id,a.name,b.name from a full outer join b
on a.id=b.id
order by id --方法二:
select a.id as id,a.name,b.name from a left join b
on a.id=b.id
union all
select b.id as id,a.name,b.name from b left join a
on a.id=b.id
order by idid name name
----------- ---------- ----------
1 aa NULL
2 NULL cc
3 bb NULL
4 NULL dd(所影响的行数为 4 行)