I have two tables: all_UnitData and all_TrooperData which has 2 columns each. One is a Varchar column and the other is a JSON object. I have attached the two tables. PS: I have limited the rows to 5. I want to combine these two tables(inner join) and the only common value is one of the values in the JSON object (STUID). How can I inner join these two tables using a JSON object value?
Here is my query:
Select JSON_Extract(TrooperInfo,'$.A') as 'STID', JSON_Extract(TrooperInfo,'$.B') as 'Rank',
JSON_Extract(TrooperInfo,'$.J') as 'Role', JSON_Extract(TrooperInfo,'$.G') as 'Category',
JSON_Extract(TrooperInfo,'$.E') as 'Height', JSON_Extract(TrooperInfo,'$.F') as 'Weight',
JSON_Extract(TrooperInfo,'$.C') as 'Gender', JSON_Extract(TrooperInfo,'$.H') as 'Status',
JSON_Extract(TrooperInfo,'$.I') as 'STUID',
JSON_Extract(STUnitInfo,'$.A') as 'BGID', JSON_Extract(STUnitInfo,'$.B') as 'Designation',
JSON_Extract(STUnitInfo,'$.E') as 'STUID2'
**FROM all_TrooperData INNER JOIN all_UnitData ON concat(all_UnitData,'.',JSSTUID = all_TrooperData.STID**
(this is where i have an issue)
I think that you want:
SELECT
td.TrooperInfo ->> '$.A' as STID,
td.TrooperInfo ->> '$.B' as Rank,
td.TrooperInfo ->> '$.J' as Role,
td.TrooperInfo ->> '$.G' as Category,
td.TrooperInfo ->> '$.E' as Height,
td.TrooperInfo ->> '$.F' as Weight,
td.TrooperInfo ->> '$.C' as Gender,
td.TrooperInfo ->> '$.H' as Status,
td.TrooperInfo ->> '$.I' as STUID,
ud.STUnitInfo ->> '$.A' as BGID,
ud.STUnitInfo ->> '$.B' as Designation,
ud.STUnitInfo ->> '$.E' as STUID2
FROM all_TrooperData td
INNER JOIN all_UnitData ud
ON ud.STUnitInfo ->> '$.E' = td.TrooperInfo ->> '$.I'
Notes:
the ->>
operator shortens the query - and it properly unquotes JSON values, which you probably want
table aliases make the query easier to read and write
do not surround column aliases with single quotes - they are meant for literal stings
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.