简体   繁体   中英

mysql json_extract dynamic position

SELECT id, JSON_EXTRACT(column,'$.parent[3].child') AS child FROM table 
WHERE JSON_EXTRACT(column,'$.parent[3].child') IN (1)

this is my sql query. i have a also column jsonlength how can i connect this 2 columns in one query? i want to change 3 to dynamic number given in jsonlength

for example:

SELECT id, JSON_EXTRACT(column,'$.parent['jsonlength'].child') AS child FROM table 
WHERE JSON_EXTRACT(column,'$.parent['jsonlength'].child') IN (1)

but this query not working.

You need to form the JSON path with string concatenation. In MySQL, this is done with the CONCAT() function .

SELECT id, JSON_EXTRACT(column, CONCAT('$.parent[',jsonlength,'].child')) AS child FROM table 
WHERE JSON_EXTRACT(column,CONCAT('$.parent[',jsonlength,'].child')) IN (1)

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.

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