简体   繁体   English

PostgreSQL-在SELECT中使用to_tsvector

[英]PostgreSQL - using to_tsvector with SELECT

I have been reading the PSQL Documentation and also bothering Google - although I am not sure what to look for - but it doesn't look it is possible to create a tsvector out of a select. 我一直在阅读PSQL文档,也打扰了Google-尽管我不确定要查找的内容-但看起来无法从选择中创建tsvector。

Let me explain a bit. 让我解释一下。 I have table users and I added a tsvector column to it called tsv. 我有表用户,并在其中添加了一个tsvector列,称为tsv。

      Column       |            Type             |                     Modifiers
-------------------+-----------------------------+---------------------------------------------------
 id                | integer                     | not null default nextval('jobs_id_seq'::regclass)
 username          | character varying(255)      | not null
 tsv               | tsvector                    |

Now every user has many articles . 现在每个用户都有很多文章

What I want now is to store the articles title as tsvector in the tsv column. 我现在想要的是将文章标题作为tsvector存储在tsv列中。 Something like this: 像这样:

UPDATE users SET tsv = to_tsvector(
  SELECT string_agg(title) 
  FROM users INNER JOIN books 
  ON user.id = articles.user_id 
  GROUP BY user.id
)

So obviously the query would not work even without trying to make a tsvector out of a SELECT. 因此,很明显,即使不尝试从SELECT中生成tsvector,查询也不起作用。 It has basically 2 "problems" 它基本上有2个“问题”

Thank you very much in advance. 提前非常感谢您。

UPDATE users
SET tsv = to_tsvector(s.tsv)
from
    (  
        SELECT id, string_agg(title) tsv
        FROM
            users
            INNER JOIN
            articles ON user.id = articles.user_id 
        GROUP BY user.id
    ) s
where users.id = s.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM