I have a column (student_no) in my database with JSON data (String).
student No: [
{First_name: 'John',
Last_name: 'Wick',
student_id: 1001,
},
{First_name: 'David',
Last_name: 'Stone',
student_id: 1002,
}
If I want to read and display only the student_id = 1002
from the data, what is the right query?
I tried using student_no.student_id = 1002
but there were error in the syntax. I want to access and display the First_name
, Last_name
by accessing the student_id
.
2
Answers
JSON data cannot be parsed with a
SELECT
statement. You’ll need to query your desired columns withSELECT
(including the JSON column) but then to use theJSON_EXTRACT
function. Check out this link for a good examples of working with JSON data in MySQL.With Javascript Code
You can use Javascript Array Filter (
.filter
) to solve that problem. Try this :With Maria DB Query
I assume your table (
my_table
) is look like this :To get the data from column
student_no
which is an array of objects bystudent_id
, just use theJSON_TABLE
query like this :The output will be :
Notes: JSON_TABLE only works starts in MariaDB 10.6.0