这语句怎么写?

dynamic8111 2010-08-18 03:13:52
表A
NAME
AAA
BBB
CCC
DDD

表B
NAME
AAA
BBB
EEE
FFF

怎么生成一张新表,把表A和B中的不同数据拿出来,如
表C
NAME
CCC
DDD
EEE
FFF

SQL语句如何写?
...全文
66 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChinaJiaBing 2010-08-18
  • 打赏
  • 举报
回复

--try

declare @a table (name nvarchar(10))
insert into @a select 'AAA'
UNION ALL SELECT 'BBB'
UNION ALL SELECT 'CCC'
UNION ALL SELECT 'DDD'
DECLARE @B TABLE (NAME NVARCHAR(10))
declare @c table (name nvarchar(10))
INSERT INTO @B SELECT 'AAA'
UNION ALL SELECT 'BBB'
UNION ALL SELECT 'EEE'
UNION ALL SELECT 'FFF'
insert into @C
select * from @a a where
not exists (select 1 from @b where a.name=name)
union all
select * from @b a where
not exists (select 1 from @a where a.name=name)
select * from @c



sanny_txx 2010-08-18
  • 打赏
  • 举报
回复
-->建立表A
if object_id('A') is not null drop table A
create table A(name nvarchar(4))
insert into A
select 'AAA'union all
select 'BBB' union all
select 'CCC' union all
select 'DDD'

-->建立表B
if object_id('B') is not null drop table B
create table B(name varchar(4))
insert into B
select 'AAA'union all
select 'BBB' union all
select 'EEE' union all
select 'FFF'

-->测试
select * from A where A.name not in (select * from B )
union
select * from B where B.name not in (select * from A)
csuxp2008 2010-08-18
  • 打赏
  • 举报
回复
select * 
from a
where not exists(select 1 from b where b.name=a.name)
union
select *
from b
where not exists(select 1 from a where a.name=b.name)
xuam 2010-08-18
  • 打赏
  • 举报
回复
select * from a where not exists (select * from b )
union
select * from b where not exists (select * from a)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧