psql 查询返回在指定值的开头/结尾处包含空格的值

use*_*195 3 whitespace space psql

我有一个包含超过 1,000,000 个条目的数据库,其中一些在值的开头/结尾处包含空格字符。

我已经尝试过以下查询并且它有效,但我必须遍历 1,000,000 条记录,因为所有 id 都是唯一的

select * from tablename where id like '%1234%';
select * from tablename where id='1234 ';
select * from tablename where id=' 1234';
select * from tablename where id=' 1234 ';
Run Code Online (Sandbox Code Playgroud)

是否有一个可以运行的查询返回在值的开头/结尾处带有空格/空字符的所有值?

感谢您的帮助 B

Her*_*III 8

您可以使用正则表达式来表示“字符串开头的一个或多个空格或字符串末尾的多个空格”。

select * from tablename where id ~ '^\s+|\s+$';
Run Code Online (Sandbox Code Playgroud)

每个特殊字符:

  • ^ 匹配字符串的开头
  • \s 匹配空白字符
  • | 运算符
  • + 一次或多次
  • $ 匹配字符串的结尾。