create table t
(column1 int column2 varchar(10) column3 varchar(10) column4 varchar(10))
insert into t
select 1 'B' 'C' 'D' union all
select 2 'B' 'C' 'E' union all
select 3 'F' 'F' 'H' select * from t a
where not exists
(select * from t where a.column2=t.column2 and a.column3=t.column3 and a.column1<t.column1)select * from t a
where a.column1 not in
(select column1 from t where a.column2=t.column2 and a.column3=t.column3 and a.column1<t.column1)
drop table t谁帮测下这个SQL 第二个SQL为什么不排除第一行啊?
(column1 int column2 varchar(10) column3 varchar(10) column4 varchar(10))
insert into t
select 1 'B' 'C' 'D' union all
select 2 'B' 'C' 'E' union all
select 3 'F' 'F' 'H' select * from t a
where not exists
(select * from t where a.column2=t.column2 and a.column3=t.column3 and a.column1<t.column1)select * from t a
where a.column1 not in
(select column1 from t where a.column2=t.column2 and a.column3=t.column3 and a.column1<t.column1)
drop table t谁帮测下这个SQL 第二个SQL为什么不排除第一行啊?
not in (select id from tb) --有多个.
not in ---是指在这个结果集中是否存在,是否和这个结果集中的值对应
from t a
where not exists
(select * from t
where a.column2=t.column2 and a.column1<t.column1 )--1 1<2 not exists为假 (条件为'b'=t.column2 and 1<t.column1 )
--2 2<??? OK (条件为'b'=t.column2 and 2<t.column1 )
--3 3<?? OK (条件为'F'=t.column2 and 3<t.column1 )
select *
from t a
where a.column1 not in
(select column1 from t
where a.column2=t.column2 and a.column1<t.column1)
--1 条件为 1 not in (2) -- OK 子查询条件 'b'=t.column2 amd 1<t.column1
--2 条件为 1 not in (??) -- OK 子查询条件 'b'=t.column2 amd 2<t.column1
--这个??不知道怎么表示,相当于一个空的数组吧
--3 同上
而
not exists 只返回1个not in返回的多个子查询记录集.. 其中一个为null 则为null
where a.column1 not in
(select a.column1 from t where a.column2=t.column2 and a.column3=t.column3 and a.column1<t.column1)
(select a.column1 from t where a.column2=t.column2 and a.column3=t.column3 and a.column1<t.column1)
你可以把
not in
和
not exists not in分解成
select * from t where col1 not in (....1<1)
union all
select * from t where col1 not in (....1<2)
union all
select * from t where col1 not in (....1<3)not exists 分解成
select * from t where not exsits
(select 1<1
union all
select 1<2
union all
select 1<3
)
当然这只是方便理解而转化的... sql 到底是怎么处理的 偶也不清楚
---------------------------not in
即:
where x not in
(
select statement
where
condition 1
AND
condition 2
..
AND
condition N
)逻辑上是 "与的非" 运算, 这个逻辑运算可转换为 "非的与"
即 _____ _ _
A * B = A * B而楼上的写法 UINION ALL 操作是 或操作,那么相当于是 将 与的非 转成 或了
即 _____ _ _
A * B = A U B取条件结果的非. 比如: A{1,2,3},B{1} 在这两个集合中,A集合除掉B集合就只剩下 2,3两个元素了.not exists
即:
where not exists
(
select statement
where
condition 1
AND
condition 2
..
AND
condition N
)
而 not exists 不是 取非运算.即并不是取条件的非.楼主的查询:
not in的写法,当低层在主查询处理第一条记录时:1 B C D子查询执行的是 select column1 from t where 'b'=column2 and 'c'=column3 and 1<column1那么在表t中 column2='b' and column3='c' 且 column1>1的,是第二条, 那么此时子查询结果集增加一行column1,值为 2, 此时子查询会再往下走,但找不到符合这个条件的记录了,接着主查询下移而 not exists的写法, 当处理第一条记录时:因为找出第二条是符合记录的,那么即exists ,子查询不会再往下看,而是退出, 接着主查询下移用not exists还是用not in 不是看子查询条件是否一样, 而是看你的应用。 因为not exists跟 not in 本来就不同
select * from t a
where a.column1 not in
(select column1 from t where a.column2=t.column2 and a.column3=t.column3 and a.column1<t.column1)
相当与
select * from t a
where a.column1 in
(select column1 from t where a.column2=t.column2 and a.column3=t.column3 and a.column1=t.column1)
也就是select * from t
select * from t a
where a.column1 not in
(select column1 from t --相当于select t.column1 from t
where a.column2=t.column2 and a.column3=t.column3 and a.column1<t.column1)而如果要和第一个查询用not exists语句的查询结果相同,应该用下面的语句
select * from t a
where a.column1 not in
(select a.column1 from t --a.column1
where a.column2=t.column2 and a.column3=t.column3 and a.column1<t.column1)NOT IN和NOT EXISTS语句完全可以等价转换,虽然在效率上有时会有差别。
where a.column1 in
(select max(column1) from t where a.column2=t.column2 and a.column3=t.column3)
select distinct * from 沧州移动G网爱立信
where 计费号码 not in
(select top 100 计费号码 from 移动G网爱立信 where 移动G网爱立信.计费号码=沧州移动G网爱立信.计费号码 )前100行没空,计费号码没空。但无返回结果。用not exists 测试有19个结果。
而用IN 有结果(虽然结果不是想要的)。