【问题标题】:How to cross join with json and get all data如何与json交叉连接并获取所有数据
【发布时间】:2022-09-27 18:38:26
【问题描述】:

我正在尝试在 mySQL 请求中使用连接函数。

我有两张桌子:

tbl_json 测试

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| id | data                                                                                                                                                                               | description |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1  | {\"complexProperties\":[{\"properties\":{\"key\":\"Registred\",\"Value\":\"123456789\"}},{\"properties\":{\"key\":\"Urgency\",\"Value\":\"Total\"}},{\"properties\":{\"key\":\"ImpactScope\",\"Value\":\"All\"}}]} | Some Text   |
| 2  | {\"complexProperties\":[{\"properties\":{\"key\":\"Registred\",\"Value\":\"123456788\"}},{\"properties\":{\"key\":\"Urgency\",\"Value\":\"Total\"}},{\"properties\":{\"key\":\"ImpactScope\",\"Value\":\"All\"}}]} | Some Text2  |
| 3  | {\"complexProperties\":[{\"properties\":{\"key\":\"Urgency\",\"Value\":\"Total\"}},{\"properties\":{\"key\":\"ImpactScope\",\"Value\":\"All\"}}]}                                                        | Some Text3  |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

tbl_registred

----------------------
| id        | name   |
----------------------
| 123456789 | Source |
| 123456788 | Cars   |
----------------------

我当前的查询:

select jt.id, rg.id as id_registred, rg.name, jt.description
from tbl_jsontesting jt
  cross join jsonb_array_elements(jt.data::jsonb -> \'complexProperties\') as p(props)
   join tbl_registred rg 
     on rg.id::text = (p.props -> \'properties\' ->> \'Value\')
    and p.props -> \'properties\' ->> \'key\' = \'Registred\'
;

结果 :

--------------------------------------------
| id | id_registred | name   | description |
--------------------------------------------
| 2  | 123456788    | Cars   | Some Text2  |
| 1  | 123456789    | Source | Some Text   |
--------------------------------------------

预期结果 :

--------------------------------------------
| id | id_registred | name   | description |
--------------------------------------------
| 3  |              |        | Some Text3  |
| 2  | 123456788    | Cars   | Some Text2  |
| 1  | 123456789    | Source | Some Text   |
--------------------------------------------

小提琴:https://www.db-fiddle.com/f/5Jvq4SXUpBvJsY7H3G13xm/2

    标签: postgresql


    【解决方案1】:

    在您的小提琴列中tbl_jsontesting.description 被命名为 name :) 除此之外 您可以使用full join 代替第二个连接以保留所有线路 然后将行 whitout id_registred 添加到您的第一个结果中:

    WITH all_line as (
      select jt.id, rg.id as id_registred, rg.name, jt.description
      from tbl_jsontesting jt
      cross join jsonb_array_elements(jt.data::jsonb -> 'complexProperties') as p(props)
      LEFT join tbl_registred rg 
        on rg.id::text = (p.props -> 'properties' ->> 'Value')
        and p.props -> 'properties' ->> 'key' = 'Registred'
    ),line_registred as (
      SELECT *
      FROM all_line
      WHERE id_registred IS NOT NULL
    )
    SELECT * FROM line_registred
    UNION ALL
    SELECT distinct * --distinct because of line generate by sub properties in json
       FROM all_line
       WHERE id NOT IN (SELECT id FROM line_registred)
    

    可能不是最短的方法,但你有你所期望的:

    | id  | id_registred | name   | description |
    | --- | ------------ | ------ | ----------- |
    | 1   | 123456789    | Source | Some Text   |
    | 2   | 123456788    | Cars   | Some Text2  |
    | 3   |              |        | Some Text3  |
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-23
      • 2019-03-02
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 2017-05-11
      相关资源
      最近更新 更多