主题:求助SQL查询问题
piaoyexie
[专家分:1380] 发布于 2006-10-29 23:17:00
环境 SQL 2000
所用数据库:pubs
涉及表:authors,publishers
查询内容:查询没有出版社的州名和该州的所有作者名。
use pubs
go
select state,au_lname,au_fname
from authors
where state not in
(
select state from authors
)
go
为什么上面的查询方法查不出来?
应该如何查询才正确?
回复列表 (共15个回复)
11 楼
Leo64823900 [专家分:960] 发布于 2006-11-06 11:41:00
select state,au_lname,au_fname
from authors
where state not in
(
select distinct state from publishers
where state is not null
)
12 楼
piaoyexie [专家分:1380] 发布于 2006-11-06 22:32:00
谢谢楼上的朋友
能否解释下为什么要加“where state is not null”这句啊?
13 楼
Leo64823900 [专家分:960] 发布于 2006-11-07 01:04:00
该句是为了清除 Publishers 表中 state 为空(Null)的纪录。 因为当表达式与 Null 比较时, 结果不确定. 请参看以下 T-SQL 关于比较运算符(=)的说明:
Compares two expressions (a comparison operator). When you compare nonnull expressions, the result is TRUE if both operands are equal; otherwise, the result is FALSE. If either or both operands are NULL and SET ANSI_NULLS is set to ON, the result is NULL. If SET ANSI_NULLS is set to OFF, the result is FALSE if one of the operands is NULL, and TRUE if both operands are NULL.
14 楼
piaoyexie [专家分:1380] 发布于 2006-11-07 12:29:00
谢谢。。。。
15 楼
EdgarSun [专家分:80] 发布于 2006-11-14 21:45:00
study...
我来回复