3

我有三个表(这里简化):

recipients: recipientId, isGroup
users: userId, first name, last name
groups: groupid, groupname

如果收件人是用户,我想检索名字/姓氏,如果收件人是一个组,我想检索组名。收件人可能两者都不是(即已删除/不再存在的组),因此在这种情况下,我不想返回任何内容。

所以这就是我所做的:

select u.first_name, u.last_name, g.groupname, r.isGroup
from recipients r
left join users u on u.userid = r.recipientId
left join groups g on g.groupid = r.recipientId
where r.isGroup IS NULL or g.groupname IS NOT NULL

上述查询未返回预期结果。我得到了这个:

adam, smith, null, null
yolanda, smith, null, null
null, null, members, 1
null, null, null, 1

最后一行是出乎意料的,因为显然没有组名(g.groupname 为 NULL)并且 r.IsGroup = 1。

当我这样做时:

select u.first_name, u.last_name, g.groupname, r.isGroup
from recipients r
left join users u on u.userid = r.recipientId
left join groups g on g.groupid = r.recipientId
where r.isGroup IS NULL

我得到预期的结果:

adam, smith, null, null
yolanda, smith, null, null

当我这样做时:

select u.first_name, u.last_name, g.groupname, r.isGroup
from recipients r
left join users u on u.userid = r.recipientId
left join groups g on g.groupid = r.recipientId
where g.groupname IS NOT NULL

我得到预期的结果:

null, null, members, 1

只有当我将两个 where 子句与 OR 结合时,我才能获得额外的行。

现在,当我将查询更改为(从 IS NULL 更改为 ISNULL)时:

select u.first_name, u.last_name, g.groupname, r.isGroup
from recipients r
left join users u on u.userid = r.recipientId
left join groups g on g.groupid = r.recipientId
where r.isGroup IS NULL or ISNULL(g.groupname, null) IS NOT NULL

我得到了预期的结果:

adam, smith, null, null
yolanda, smith, null, null
null, null, members, 1

事实上,我什至不必更改 where 子句,以下查询也同样有效,并为我提供了如上所示的预期结果:

select u.first_name, u.last_name, ISNULL(g.groupname,null), r.isGroup
from recipients r
left join users u on u.userid = r.recipientId
left join groups g on g.groupid = r.recipientId
where r.isGroup IS NULL or g.groupname IS NOT NULL

所以,问题是,为什么?为什么将 ISNULL 放在我的 SELECT 语句中会改变 WHERE 子句的工作方式?为什么它会有所作为?为什么我的第一个查询不起作用?为什么只有当我添加 OR 时它才会起作用 - 为什么没有它它也不会损坏?

提前致谢。

我正在使用 MS SQL Server 2008。

编辑:修正错字,澄清问题

4

5 回答 5

4

我们发现这是未安装服务包的 Sql Server 2008 中的一个问题。尝试安装 SP1 或 SP2。在我们的例子中,服务包解决了这个问题。

于 2011-09-22T18:15:15.280 回答
2

ISNULL 是一个函数,如果列为空,则返回指定的输入值,否则返回列值。

ISNULL(tbl.x, 'y') --returns 'y' if tbl.x is null otherwise the value of tbl.x
于 2011-09-14T19:29:35.740 回答
2

我相信你想要做的是获取所有用户和组,如果它是用户 first_name 和 last_name 将是某事并且 groupname 将是 NULL,但如果它是一个组,first_name 和 last_name 将是 NULL 并且 groupname 将是某事. 相反,您的 WHERE 子句过滤结果,而不是定义数据连接方式的条件。我认为这会得到你正在寻找的东西(假设r.isGroup == 1它是一个组):

要或多或少地使用原始查询但消除不作为组或用户存在的记录:

select u.first_name, u.last_name, g.groupname, r.isGroup
from recipients r
left join users u 
    on u.userid = r.recipientId and r.isGroup != 1
left join groups g 
    on g.groupid = r.recipientId and r.isGroup == 1
where u.userid IS NOT NULL or g.groupid IS NOT NULL

但是,我认为带有内部连接的联合会表现得更好:

select u.first_name, u.last_name, NULL, r.isGroup
from recipients r
inner join users u on u.userid = r.recipientId
where r.isGroup != 1
union
select NULL, NULL, g.groupname, r.isGroup
from recipients r
inner join groups g on g.groupid = r.recipientId
where r.isGroup == 1
于 2011-09-14T19:40:18.257 回答
0

从 SELECT 子句:

u.groupname

从 WHERE 子句:

g.组名

让这两个匹配,让我们知道您是否看到任何不同。

于 2011-09-14T19:33:18.180 回答
0

这似乎可以得到您想要的结果,尽管如果 NULLS swiss-cheesing 您的数据有任何问题,它可能无法正常工作:

select u.first_name, u.last_name, g.groupname, r.isGroup
from #recipients r
left join #users u on u.userid = r.recipientId
left join #groups g on g.groupid = r.recipientId
WHERE  g.groupname IS NOT NULL 
    OR ISNULL(r.IsGroup,0) > CASE WHEN g.groupid IS NOT NULL AND g.groupname IS NULL THEN -1 ELSE 0 END
于 2011-09-14T20:13:10.677 回答