【发布时间】: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 |
--------------------------------------------
标签: postgresql