陋室铭
永远也不要停下学习的脚步(大道至简至易)

SQL SERVER 不支持多字段的IN 和 NOT IN    但是ORACLE是支持的。


表a  有字段:a, b, c还可能有其他字段。

表b  有字段:a,b,c 还可能有其他字段。

create table a (
a varchar(100),
b varchar(100),
c varchar(100)
)
create table b (
a varchar(100),
b varchar(100),
c varchar(100)
)


insert a values ('1','a','甲');
insert a values ('2','b','乙');
insert a values ('3','c','丙');


insert b values ('1','a','甲');
insert b values ('2','b','乙');

insert b values ('3','c','丙');


执行该语句:select * from a where (a,b) IN (select a,b from b);

结果:

消息 102,级别 15,状态 1,第 2 行
',' 附近有语法错误。



语句改成 exists :

select * from a
where exists (select 1 from b
where a.a=b.a and a.b = b.b
 );


执行结果:



所以:在sqlserver 中要使用多字段的in 或者是 not in  应该改为 exists 或者 not  exists 语句。

posted on 2020-05-08 11:43  宏宇  阅读(2237)  评论(0编辑  收藏  举报