(最初是 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/