postgresql - Postgres 中的运算符 ~<~

标签 postgresql sorting pattern-matching operators

(最初是 this question 的一部分,但有点无关紧要,所以我决定把它作为一个单独的问题。)

我找不到运算符 ~<~是。 Postgres 手册只提到 ~和类似的运营商here , 但没有 ~<~ 的迹象.

在 psql 控制台中摆弄时,我发现这些命令给出了相同的结果:

SELECT * FROM test ORDER BY name USING ~<~;
SELECT * FROM test ORDER BY name COLLATE "C";

这些给出了相反的顺序:

SELECT * FROM test ORDER BY name USING ~>~;
SELECT * FROM test ORDER BY name COLLATE "C" DESC;

还有一些关于波浪号运算符的信息:

\do ~*~
                                    List of operators
  Schema   | Name | Left arg type | Right arg type | Result type |       Description       
------------+------+---------------+----------------+-------------+-------------------------
pg_catalog | ~<=~ | character     | character      | boolean     | less than or equal
pg_catalog | ~<=~ | text          | text           | boolean     | less than or equal
pg_catalog | ~<~  | character     | character      | boolean     | less than
pg_catalog | ~<~  | text          | text           | boolean     | less than
pg_catalog | ~>=~ | character     | character      | boolean     | greater than or equal
pg_catalog | ~>=~ | text          | text           | boolean     | greater than or equal
pg_catalog | ~>~  | character     | character      | boolean     | greater than
pg_catalog | ~>~  | text          | text           | boolean     | greater than
pg_catalog | ~~   | bytea         | bytea          | boolean     | matches LIKE expression
pg_catalog | ~~   | character     | text           | boolean     | matches LIKE expression
pg_catalog | ~~   | name          | text           | boolean     | matches LIKE expression
pg_catalog | ~~   | text          | text           | boolean     | matches LIKE expression
(12 rows)

第三行和第四行是我要找的运算符,但描述对我来说有点不够。

最佳答案

~>=~ , ~<=~ , ~>~~<~text模式(或 varchar ,基本相同)运算符,它们各自 sibling 的对应物 >= , <= , >< .它们严格按照字节值对字符数据进行排序,忽略任何排序规则设置的规则(与它们的 sibling 相反)。这使它们更快,但对大多数语言/国家也无效。

“C”语言环境实际上与无语言环境 相同,意味着没有排序规则。这解释了为什么 ORDER BY name USING ~<~ORDER BY name COLLATE "C"最终做同样的事情。后一种语法变体应该是首选:更标准,更不容易出错。

dba.SE 上这个相关回答的最后一章有详细解释:

请注意 ~~/~~* LIKE / ILIKE 的 Postgres 运算符与上述几乎没有关系。同样,!~~/!~~*对于 NOT LIKE/NOT IILKE . (使用标准 LIKE 符号代替这些“内部”运算符。)

相关:

关于postgresql - Postgres 中的运算符 ~<~,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35807872/

相关文章:

postgresql - 授予了我的 PostGres 表的所有权限,但在尝试插入/选择时仍然出现 "Permission denied"错误

java - 我的程序似乎写得很完美,但停止运行并允许用户在没有扫描仪的情况下输入?

JavaScript 正则表达式全局匹配组

javascript - 如何在没有模式匹配的情况下用动态类型语言实现和类型

json - 在 postgres 中选择或提取 jsonb 数据内的所有键值

php - Doctrine (postgresql) Pessimistic Locking - 不会抛出 PessimisticLockException

java - 对文件中的数据进行排序

c# - 如何对treeView的子节点进行排序

pattern-matching - 在 Fin 图案中使用文字数字

ruby-on-rails - 使用 zeus : Postgres is being accessed by other users 进行 cucumber 和 RSpec 测试