如何作为JSON获得后Gresql查询结果

发布于 2025-02-06 03:19:48 字数 928 浏览 3 评论 0 原文

我在表格中有文本字段,我想使用条件的地方查询此字段:​​我想查询所有至少一个单词as as as s sords as s of单词并返回JSON的记录,并返回了这样的JSON:

       text
The employee was fired today
He likes chocolate a lot
She eat chocolate today
Car was stolen yesterday

select * from tbl
where text CONTAINS ANY ['today','likes','eat']

Guestered输出1:

{"id":"1", "text":"The employee was fired today", "tag":"today"}
{"id":"2", "text":"He likes chocolate a lot",     "tag":"likes"}
{"id":"3", "text":"She eat chocolate today",      "tag":["today","eat"]}

Desidered输出2:

    text                         tag             tag_counts
The employee was fired today    today               1
He likes chocolate a lot        likes               1
She eat chocolate today         eat, today          2

我想获得这些输出中的任何一个。

我已经发现我可以使用在其中('今天','likes','eat'),但如果可能的话,我找不到如何在任何所需的输出中获得结果。

I have text field in a table and I want query this field using where a condition: I want to query all records that has at least one word from as list of words and returns a JSON like this:

       text
The employee was fired today
He likes chocolate a lot
She eat chocolate today
Car was stolen yesterday

select * from tbl
where text CONTAINS ANY ['today','likes','eat']

Desidered Output 1:

{"id":"1", "text":"The employee was fired today", "tag":"today"}
{"id":"2", "text":"He likes chocolate a lot",     "tag":"likes"}
{"id":"3", "text":"She eat chocolate today",      "tag":["today","eat"]}

Desidered Output 2:

    text                         tag             tag_counts
The employee was fired today    today               1
He likes chocolate a lot        likes               1
She eat chocolate today         eat, today          2

I would like to get any of these outputs.

I already found that I can use WHERE IN ('today','likes','eat') but I can't find out how to get the result in any of the desired output, if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪也成诗 2025-02-13 03:19:48

我选择了文本列的列名单词。 “文本”是一个基本的类型名称,因此太令人困惑了。

对于带有平原 文本 列的给定表格:

SELECT *
FROM   tbl t
, LATERAL (
   SELECT string_agg(tag, ', ') AS tags, count(*) AS tag_count
   FROM  (
      SELECT unnest(string_to_array(words, ' ')) tag
      INTERSECT ALL
      SELECT unnest ('{today, likes, eat}'::text[])
      ) i
   ) ct
WHERE  string_to_array(t.words, ' ') && '{today, likes, eat}';

更简单的文本数组( text [] )表格从:

SELECT *
FROM   tbl1 t
, LATERAL (
   SELECT string_agg(tag, ', ') AS tags, count(*) AS tag_count
   FROM  (
      SELECT unnest(words) tag
      INTERSECT ALL
      SELECT unnest ('{today, likes, eat}'::text[])
      ) i
   ) ct
WHERE  t.words && '{today, likes, eat}';

db<> fiddle

可以用 gin索引支持。 text 列的表达式索引:

CREATE INDEX tbl_words_gin_idx ON tbl USING gin (string_to_array(words, ' '));

text []

CREATE INDEX tbl1_words_gin_idx ON tbl1 USING gin (words);

See:

I chose the column name words for the text column. "text" is a basic type name and too confusing as such.

For your given table with a plain text column:

SELECT *
FROM   tbl t
, LATERAL (
   SELECT string_agg(tag, ', ') AS tags, count(*) AS tag_count
   FROM  (
      SELECT unnest(string_to_array(words, ' ')) tag
      INTERSECT ALL
      SELECT unnest ('{today, likes, eat}'::text[])
      ) i
   ) ct
WHERE  string_to_array(t.words, ' ') && '{today, likes, eat}';

Simpler with a text array (text[]) in the table to begin with:

SELECT *
FROM   tbl1 t
, LATERAL (
   SELECT string_agg(tag, ', ') AS tags, count(*) AS tag_count
   FROM  (
      SELECT unnest(words) tag
      INTERSECT ALL
      SELECT unnest ('{today, likes, eat}'::text[])
      ) i
   ) ct
WHERE  t.words && '{today, likes, eat}';

db<>fiddle here

Can be supported with a GIN index. An expression index for the text column:

CREATE INDEX tbl_words_gin_idx ON tbl USING gin (string_to_array(words, ' '));

Simpler, yet again, for text[]:

CREATE INDEX tbl1_words_gin_idx ON tbl1 USING gin (words);

See:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文