sql合并语句
新手,急求教如下语句
表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
得出结果的语句怎么写啊?
------解决方案--------------------select 表1.id,字段1,字段2 From 表1
left join 表2 on 表1.id=表2.id
union
select 表2.id,字段1,字段2 From 表1
right join 表2 on 表1.id=表2.id
------解决方案--------------------不需要用Union,可以直接用FULL JOIN
Select
IsNull(A.id, B.id) As id,
A.字段1,
B.字段2
From
表1 A
Full Join
表2 B
On A.id = B.id
------解决方案--------------------ls正解~
------解决方案--------------------select * from a,b
where a.*=b.*
------解决方案-------------------- create table a (id int,name varchar(10))
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 id
id name name
----------- ---------- ----------
1 aa NULL
2 NULL cc
3 bb NULL
4 NULL dd
(所影响的行数为 4 行)
------解决方案--------------------ojuju10(longdchuanren)是明白人