在子查询的时候 想 not in (字段1,字段2) 可以么?
或者怎么写呢?
或者怎么写呢?
解决方案 »
- 怎样按一定规则拆分字符串?
- 如何分出字符里哪些是汉字,哪些是字母,最后留下汉字
- MS SQL聚合函数
- 当只有begin tran和commit tran时会不会全部回滚?
- 象这种自动编号,是如何生成的?a8efc285-f0b1-4f8f-8e73-2b7d8724a47c
- 太奇怪了,各位帮忙看看数据库服务器为什么不能访问?
- java.sql.SQLException: [Microsoft][SQLServer JDBC Driver]The hexadecimal string
- ASP+SQL是怎么连起来的?
- 在配置sqlmail的时候,如用的是outlook express ,该怎么设置邮件配置文件?
- MSSQL: 求最新10条记录之和
- 一个在百度知道看到的查询语句目前做不出来求高手解决
- 如何查询每行记录往后8天之内是否再次出现?
如果楼主想要查询不在字段1或者不在字段2中的记录,可以采取下面的方式
SELECT .....
FROM LI
WHERE ID NOT IN
(SELECT ID1 AS ID FROM LI1 UNION
SELECT ID2 FROM LI1)
select * from tb where id not in(select 字段1 from .....) and id not in(select 字段2 from .....)
if object_id('[tab]') is not null drop table [tab]
create table [tab]([no] int,[partno] int,[other] varchar(5),[num1] int)
insert [tab]
select 1,2,'abcde',2 union all
select 1,2,'mmm',0 union all
select 1,3,'kkk',1 union all
select 1,4,'mkl',5select * from tab where partno not in(2,4)
/*
no partno other num1
----------- ----------- ----- -----------
1 3 kkk 1(所影响的行数为 1 行)*/select * from tab t where partno in (select partno from tab where partno not in(2,4))
/*
no partno other num1
----------- ----------- ----- -----------
1 3 kkk 1(所影响的行数为 1 行)*/
not in()这里应该是一个集合。
你要么把not in 的那个集合表示出来
select a from table where a not in
(select 字段1 from table union all
select 字段2 from table
)s
我现在是 select × from t1 where 字段1 not in (select temp1 from t2) and 字段1 not in (select temp2 from t2)
但是我感觉不效率 因为后面的子查询 条件很多, 语句很长。 想知道怎么写比较好,
select ×
from t1
where not exists(
select 1
from t2
where temp1=t1.字段1 or temp2=t1.字段1)
SELECT * FROM TB t WHERE NOT EXISTS(SELECT * FROM TB WHERE [字段1]=t.[字段1] AND [字段2]=t.[字段2])
2.如果不需要太精确地情况下可以使用CHECKSUM。
SELECT * FROM TB WHERE CHECKSUM([字段1],[字段2]) NOT IN (SELECT CHECKSUM([字段1],[字段2] FROM TB)
select ×
from t1
where not exists(
select 1
from t2
where temp1=t1.字段1
and temp2=t1.字段1)or应该用and
这样写可以么?
EXISTS Returns TRUE if a subquery returns at least one row.
SORRY 我误导了你
应该用
SELECT * FROM TB t WHERE NOT EXISTS(SELECT * FROM TB WHERE [字段1]=t.[字段1] AND [字段2]=t.[字段2])
select * from t1 where Not exists (select * from t2 where t1.字段1=temp1 or t1.字段1=temp2 )
1.NOT EXISTSSQL codeSELECT * FROM TB t WHERE NOT EXISTS(SELECT * FROM TB WHERE [字段1]=t.[字段1] AND [字段2]=t.[字段2])这个我试过了,顶