运营商〜<〜在Postgres

时间:2016-03-04 23:22:11

标签: 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)

第3行和第4行是我正在寻找的运算符,但描述对我来说有点不足。

1 个答案:

答案 0 :(得分:4)

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

“C”语言环境实际上与 no locale 相同,这意味着没有整理规则。这解释了为什么ORDER BY name USING ~<~ORDER BY name COLLATE "C"最终会做同样的事情。

关于dba.SE的相关答案的最后一章的详细解释:

请注意, ~~ 是用于实现SQL LIKE expression的Postgres运算符,几乎不相关。同样,~~*实现ILIKE。更多: