称为table1的表具有名为field1的字段,其中包含空值。此查询不返回任何行:包含空值的字段的SQL表
SELECT *
FROM table1
WHERE field1 NOT IN
(
SELECT field1
FROM table1
)
我知道有更好的方法来编写此查询。造成这种行为的原因是,不使用包含空值的字段。
称为table1的表具有名为field1的字段,其中包含空值。此查询不返回任何行:包含空值的字段的SQL表
SELECT *
FROM table1
WHERE field1 NOT IN
(
SELECT field1
FROM table1
)
我知道有更好的方法来编写此查询。造成这种行为的原因是,不使用包含空值的字段。
OK,我会在这里失去了点,但重组你查询的一种方法是:
第1步:取table1.field1
所有值。
步骤2:返回所有table1
行其field1
的值不在步骤1
当然这总是返回空集得到的值中?
谢谢。这是一个例子。插入TestTable值(1,'book',8) insert into TestTable values(2,'video',7) insert into TestTable values(1,'book',8)创建表TestTable(id int,type varchar(100),indexvalue integer) insert into TestTable values (3,'video',null) select * from testtable where type ='book' and indexvalue not in(select testvalue from testtable where type ='video') 我期望一行返回上面的查询,但没有。我相信这 是因为'indexvalue'中有空值。 – w0051977
在假设有在表的主键字段存在,你可以使用这个脚本:
select
t1.*
from
dbo.Table1 as t1
where
t1.field1 = 'Value1'
and not exists(select 1 from Table1 as t2 where t1.ID != t2.ID and t2.field1 = 'Value2')
什么是你希望这个查询返回? – NPE
表格没有正常化(我知道这不是一个好主意,但现在我没有办法做到这一点)。我试图运行的查询如下所示:SELECT * FROM table1其中field1 ='Value1'和field1不在(从table1 where field1 ='value2'中选择field1)。我试图找到表1中的所有行,其中field1 = value1,但从结果集中排除表中其他行上的field1 = value2。我意识到有更好的方式来编写这个查询。我的问题是关于使用NOT IN与包含空值的列。 – w0051977
此表是否有主键字段? –