简体   繁体   English

PostgreSQL 中的 JSON 数组的复杂查询

[英]Complex query with JSON array in PostgreSQL

I've got a view in my Postgres DB with word/clue pairs for puzzles and puzzle IDs, called "allwords":我在我的 Postgres DB 中有一个视图,其中包含用于拼图和拼图 ID 的单词/线索对,称为“allwords”:

    word  |  clue       |  puz_id
    =============================
    dog   |  animal     |  1
    -----------------------------
    cat   |  animal     |  1
    -----------------------------
    apple |  fruit      |  2
    -----------------------------
    etc...

Now I want to see the matches in the DB against an array of word/clue pairs passed in a JSON string, which may look like现在我想查看数据库中与 JSON 字符串中传递的单词/线索对数组的匹配,这可能看起来像

[{"word": "dog", "clue": "%animal%"}, {"word": "cat", "clue": "%ani%"}, {"word": "appl%", "clue": "fruit"}]

I use jsonb_to_recordset in PostgreSQL to convert the JSON into a table, then my final query looks as follows:我在 PostgreSQL 中使用jsonb_to_recordset将 JSON 转换为表,然后我的最终查询如下所示:

select a."word", a."clue", count(a.puz_id) as "matched"
from allwords a
where exists (
    select 1 from 
    jsonb_to_recordset('[{"word": "dog", "clue": "%animal%"}, {"word": "cat", "clue": "%ani%"}, {"word": "appl%", "clue": "fruit"}]'::jsonb) 
    as jsdata("word" text, clue text)
    where a."word" ilike jsdata."word" and a."clue" ilike jsdata.clue
    )
group by a."word", a."clue"
order by "matched" desc;

The result I get now is grouped by the matching words/clues from the DB table:我现在得到的结果按数据库表中的匹配词/线索分组:

    word  |  clue       |  matched
    =============================
    dog   |  animal     |  5 (e.g.)
    -----------------------------
    cat   |  animal     |  3 (e.g.)
    -----------------------------
    apple |  fruit      |  1 (e.g.)
    -----------------------------

The question is: how can I get a similar match table grouped by both matching and matched word/clue pairs?问题是:我怎样才能得到一个由匹配匹配的单词/线索对分组的类似匹配表? I expect something like:我期待类似的东西:

    matched_word  |  matched_clue      | matching_word  |  matching_clue       |  matched
    =======================================================================================
    dog           |  %animal%          | dog            |  animal              |  5
    ---------------------------------------------------------------------------------------
    cat           |  %ani%             | cat            |  animal              |  3
    ---------------------------------------------------------------------------------------
    appl%         |  fruit             | apple          |  fruit               |  1
    ---------------------------------------------------------------------------------------

Got that!了解! Query should use join on ilike :查询应在ilike上使用join

select jsdata."word", jsdata.clue, count(a.puz_id) as "matched" from 
allwords a
join 
    jsonb_to_recordset('[{"word": "dog", "clue": "%animal%"}, {"word": "cat", "clue": "%ani%"}, {"word": "appl%", "clue": "fruit"}]'::jsonb) 
    as jsdata("word" text, clue text)
    on (a."word" ilike jsdata."word" and a.clue ilike jsdata.clue) -- THIS WORKS!!!
group by jsdata."word", jsdata.clue
order by "matched" desc;

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

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