在Postgres的WHERE中使用正则表达式 [英] Using regex in WHERE in Postgres

查看:102
本文介绍了在Postgres的WHERE中使用正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前有以下查询:

select regexp_matches(name, 'foo') from table;

我该如何重写它,使正则表达式位于如下所示的位置(不起作用):

How can I rewrite this so that the regex is in the where like the following (not working):

select * from table where regexp_matches(name, 'foo');

当前错误消息是:
错误:WHERE的参数必须为布尔型,而不是类型text []
SQL状态:42804
字符:29

Current error message is: ERROR: argument of WHERE must be type boolean, not type text[] SQL state: 42804 Character: 29

推荐答案

写成:

select * from table where name ~ 'foo'

$ b的表中选择*
$ b

'〜'运算符会生成一个布尔值,用于确定正则表达式是否匹配,而不是提取匹配的子组。

The '~' operator produces a boolean result for whether the regex matches or not rather than extracting the matching subgroups.

这篇关于在Postgres的WHERE中使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆