Postgres 中的运算符 ~<~

Operator ~<~ in Postgres

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

我找不到运算符 ~<~ 是什么。 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)

第3行和第4行是我要找的算子,但是描述对我来说有点不够

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

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

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

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

相关:

  • ~~ Operator In Postgres
  • Symfony2 Doctrine - ILIKE clause for PostgreSQL?