如何计算PostgreSQL中字符串中子字符串的出现次数?
例:
我有一张桌子
CREATE TABLE test."user"
(
uid integer NOT NULL,
name text,
result integer,
CONSTRAINT pkey PRIMARY KEY (uid)
)
Run Code Online (Sandbox Code Playgroud)
我想编写一个查询,以便result包含列o列中name包含的子字符串的出现次数.例如,如果在一行中,name则hello world列result应该包含2,因为o字符串中有两个hello world.
换句话说,我正在尝试编写一个可以作为输入的查询:
并更新result列:
我知道函数regexp_matches及其g选项,它指示g需要扫描完整(=全局)字符串是否存在所有出现的子字符串).
例:
SELECT * FROM regexp_matches('hello world', 'o', 'g');
Run Code Online (Sandbox Code Playgroud)
回报
{o}
{o}
Run Code Online (Sandbox Code Playgroud)
和
SELECT COUNT(*) FROM regexp_matches('hello world', 'o', 'g');
Run Code Online (Sandbox Code Playgroud)
回报
2
Run Code Online (Sandbox Code Playgroud)
但我没有看到如何编写一个UPDATE查询来更新result列,使其包含列name …