I have two tables, and I want to use a value from primary table (here: person.ref
) to extract some data from a json
field in a secondary table (here: person_details
):
create table person (
id int(8),
ref varchar(20),
primary key (id)
);
create table person_details (
id int(8),
details json,
primary key (id)
);
SELECT JSON_EXTRACT(details, CONCAT('$.', p.ref))
FROM person p
JOIN person_details d using (id);
The person_details data is like:
{
"ref1": {
...
}
}
But that does not matter, because the sql statement itself seems to be invalid:
Result:
#3143 - Invalid JSON path expression. The error is around character position 12.
Why?
2
Answers
While the answer above is correct, it did not solve my issue, but I had to add doublequotes around
p.ref
, so that the concatenation results inCONCAT($."p.ref")
:Your code should work on MySQL >= 5.7. Check this dbfiddle:
https://dbfiddle.uk/esEJ9uHd