Not In 查询问题

metaphy 2011-04-02 10:38:37
我有一些数据 A,B,D,F...
想跟表中记录 A,B,C,X,Y... 比较
查出哪些记录不再table中 (比如D,F....)

请问这个语句怎么写?谢谢!
...全文
161 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
飘零一叶 2011-04-02
  • 打赏
  • 举报
回复
OPENROWSET
metaphy 2011-04-02
  • 打赏
  • 举报
回复
不好意思,忘记加了。
我只有查询权限,无法update/insert以及建临时表。
AcHerat 元老 2011-04-02
  • 打赏
  • 举报
回复
select distinct a1 from TB1 where a1 not in (select distinct a2 from TB2)
yy1987316 2011-04-02
  • 打赏
  • 举报
回复
我有一些数据 A,B,D,F...
--导入到表TB1中
想跟表中记录 A,B,C,X,Y... 比较
--假设这个表叫做TB2
查出哪些记录不再table中 (比如D,F....)
请问这个语句怎么写?谢谢!

select distinct a1 from TB1 where a1 not in (select a2 from TB2)
--小F-- 2011-04-02
  • 打赏
  • 举报
回复
把你那一堆数据放进一个表中

select * from ta t where not exists(select 1 from table where id=t.id)
dawugui 2011-04-02
  • 打赏
  • 举报
回复
[Quote=引用楼主 metaphy 的回复:]
我有一些数据 A,B,D,F...
想跟表中记录 A,B,C,X,Y... 比较
查出哪些记录不再table中 (比如D,F....)

请问这个语句怎么写?谢谢!
[/Quote]
select * from ta where id not in (select id from tb)
天-笑 2011-04-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 acherat 的回复:]
SQL code

--把这些数据插入临时表里

select b.*
from #tb b
where val not in (select distinct val from tb)
[/Quote]
1楼正解
AcHerat 元老 2011-04-02
  • 打赏
  • 举报
回复

--把这些数据插入临时表里

select b.*
from #tb b
where val not in (select distinct val from tb)
gogodiy 2011-04-02
  • 打赏
  • 举报
回复

create table t1
(
col1 varchar(10)
)
insert into t1
select 'A' union all
select 'B' union all
select 'C' union all
select 'X' union all
select 'Y'
--已经存在的表,不用你创建

declare @abc table(col1 varchar(10)) --定义一个表变量
insert into @abc
select 'A' union all
select 'B' union all
select 'D' union all
select 'F'
select * from @abc where col1 not in (select col1 from t1)

如果你没有权限INSERT/UPDATE和创建临时表,那么就用表变量。

34,576

社区成员

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

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