两张表某个字段取唯一的SQL语句怎么写?

wqbendan 2008-01-16 10:43:40
现在我有两张表ypdebt和spdebt字段如下
表1.A字段 表2.A字段
101 101
102 105
104 106
我现在要得到一个新表
新表.A字段
101
102
104
105
106
就是要两个表的A字段相加然后取唯一,请问SQL语句怎么写?请各位大侠指教啊?
...全文
166 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wqbendan 2008-01-16
  • 打赏
  • 举报
回复
搞定了,谢谢了
wlzd3636 2008-01-16
  • 打赏
  • 举报
回复
还以为是沙发 晕好快
wlzd3636 2008-01-16
  • 打赏
  • 举报
回复
方法1:
select a字段 from 表1
union
select a字段 from 表2

方法2:
inset into 表1 select * from 表2 not exists(select * from 表1 where 表1.a字段=表2.a字段)
select * from 表1

你也可以用临时表或变量表

抢个沙发先

areswang 2008-01-16
  • 打赏
  • 举报
回复
insert into 表1 select * from 表2 where id not exists 表1
kk19840210 2008-01-16
  • 打赏
  • 举报
回复

select A from ypdebt
union
select a from spdebt
kk19840210 2008-01-16
  • 打赏
  • 举报
回复

select A from ypdebt
union
select a from spdebt
wuxinyuzhu 2008-01-16
  • 打赏
  • 举报
回复
select 字段 from 表1
union
select 字段 from 表2
utpcb 2008-01-16
  • 打赏
  • 举报
回复
select a from A
union
select a from B where exists not (select*from B where A.a=B.a)
dawugui 2008-01-16
  • 打赏
  • 举报
回复
select A from tb1
union
select A from tb2
wzy_love_sly 2008-01-16
  • 打赏
  • 举报
回复
select a from 表1
union
select a from 表2
-狙击手- 2008-01-16
  • 打赏
  • 举报
回复
select a from table1
union
select a from table2

34,590

社区成员

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

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