【问题标题】:Operator ~<~ in Postgres运算符 ~<~ 在 Postgres
【发布时间】:2016-03-04 23:22:11
【问题描述】:

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

我找不到运算符~&lt;~ 是什么。 Postgres 手册只提到了~ 和类似的运算符here,但没有提到~&lt;~

在 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 行是我要找的运算符,但描述对我来说有点不足。

【问题讨论】:

标签: postgresql sorting pattern-matching operators


【解决方案1】:

~&gt;=~~&lt;=~~&gt;~~&lt;~text模式(或varchar,基本相同)运算符,它们各自兄弟的对应&gt;=&lt;=、@ 987654334@和&lt;。它们严格按字节值对字符数据进行排序,忽略任何排序规则设置(而不是它们的兄弟)。这使它们更快,但对大多数语言/国家也无效。

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

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

请注意,~~ / ~~*LIKE / ILIKE 的 Postgres 运算符,与上述几乎没有关系。同样,!~~ / !~~* 对应于 NOT LIKE / NOT IILKE。 (使用标准的LIKE 表示法而不是这些“内部”运算符。)

相关:

【讨论】:

  • 感谢您的回答,特别是指出与COLLATE "C" 的关系。在问我的问题之前,我什至在 dba.SE 上阅读了你的答案,但不知何故,我错过了 ~&lt;~ 运算符的部分。
猜你喜欢
  • 2011-02-09
  • 2010-11-15
  • 2016-02-28
  • 2017-02-05
  • 2011-08-31
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2012-08-11
相关资源
最近更新 更多