我知道sql标准允许列中的多个NULL值是UNIQUE约束的一部分.
我不明白为什么UNION构造(至少在Postgresql中)将NULL值视为相同.例如:
$select * from tmp_a; a | b ---+--- a | b a | | (3 rows) $select * from tmp_b; a | b ---+--- a | c a | | (3 rows) $select a,b from tmp_a union select a,b from tmp_b order by 1,2; a | b ---+--- a | b a | c a | | (4 rows)
解决方法
sql-92标准中的一般规则如下:
13.1’声明游标'(记住ORDER BY是游标的一部分)
一般规则3b:
the following special treatment of
null values. Whether a sort key value
that is null is considered greater or
less than a non-null value is
implementation-defined,but all sort
key values that are null shall either
be considered greater than all
non-null values or be considered less
than all non-null values.
sql-89对IMO说得更清楚一点:
Although
x = y
is unkNown if bothx
andy
areNULL
values,in the
context ofGROUP BY
,ORDER BY
anddisTINCT
,aNULL
value is
identical to or is a duplicate of
anotherNULL
value.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。