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

标签 mysql sql postgresql subquery lexicographic

我想知道当使用比较运算符(例如 >>= 执行行子查询时,比较的顺序是使用字典(即字典)顺序定义的还是按元素定义的?

也就是说,对于ROW (A, B),应该

(79, 48) > (75, 52) 

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

我在 PostgreSQL 中对此进行了测试,它似乎使用了字典顺序,即 (79, 48) > (75, 52)TRUE 因为 79 > 75 因此第二个组件并不重要。搜了一圈,MySQL好像也是这样:MySQL row subquery comparison issue ,并且 MySQL 文档在这一点上似乎令人困惑。搜索 postgresql row subquery 不会显示太多有关比较顺序的信息。

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

 (52, 75) > (48, 79)  

对于行(B,A)。比较相同的行,顺序完全相反,因为 B 列首先列出。

我的问题是:

此行为(在行查询中使用字典顺序)是来自 SQL 标准/跨供应商还是特定于实现?有这方面的引用资料吗?

最佳答案

这记录在章节 Row Constructor ComparisonPostgres 手册:

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 标准。

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

相关:

关于mysql - SQL ROW 子查询中比较的顺序定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36465058/

相关文章:

PHP 获取最大值。来自 MySql 的 1000 个条目

c# - 如何在不显式指定主键的情况下使用 Dapper Extensions 将对象插入 PostGreSql?

填写个人资料字段时,Django 用户创建在管理中失败

postgresql - Postgres : Composite fulltext/btree index

python - 字符串解析与数据库查询

php - 以数据库base64格式存储的图像如何在PHP中以原始格式检索它

mysql - Ruby on Rails database.yml 中的套接字声明是什么?

mysql - 如果表中的字段中有 1,则需要检查其他表

sql - DateDiff 输出小时和分钟

mysql - 外连接多个表