SQL ROW 子查询中比较的顺序定义?

order definition for comparisons in SQL ROW subqueries?

我想知道什么时候用>>=之类的比较运算符进行行子查询时,比较的顺序是使用字典序(即字典)顺序定义的还是它定义的元素-明智的?

即对于ROW (A, B),应该

(79, 48) > (75, 52) 

WHERE 子句的行查询中是 TRUE(字典)或 FALSE(按元素)?

我在 PostgreSQL 中对此进行了测试,它似乎使用的是字典顺序,即 (79, 48) > (75, 52)TRUE 因为 79 > 75 而第二个组件没有'因此无关紧要。搜索了一下,似乎 MySQL: 也是如此,而 MySQL 文档似乎在这一点上令人困惑。搜索 postgresql row subquery 并没有显示太多关于比较顺序的信息。

虽然从计算机科学的角度来看字典顺序是有意义的,但对于数据库用户来说它可能看起来有点奇怪,因为行的顺序现在取决于 您在 [=47] 中首先列出的列=]。例如,使用字典顺序,我们应该有:

 (52, 75) > (48, 79)  

ROW (B,A)。比较相同的行,顺序正好相反,因为第 B 列列在前面。

我的问题是:

此行为(在行查询中使用字典顺序)来自 SQL standards/cross-vendor 还是特定于实现?对此有任何参考吗?

这在 Postgres 手册的 Row Constructor Comparison 章中有记录:

For the <, <=, > and >= cases, the row elements are compared left-to-right, stopping as soon as an unequal or null pair of elements is found. If either of this pair of elements is null, the result of the row comparison is unknown (null); otherwise comparison of this pair of elements determines the result. For example, ROW(1,2,NULL) < ROW(1,3,0) yields true, not null, because the third pair of elements are not considered.

并且:

Note: Prior to PostgreSQL 8.2, the <, <=, > and >= cases were not handled per SQL specification. A comparison like ROW(a,b) < ROW(c,d) was implemented as a < c AND b < d whereas the correct behavior is equivalent to a < c OR (a = c AND b < d).

这表明现代 Postgres 的行为符合 SQL 标准。

这与 SELECT 查询的 ORDER BY 子句中的逻辑基本相同:项目从左到右进行比较,直到找到第一个不等式 - NULL 值除外,它排序 last 默认升序。

相关: