Answer the question
In order to leave comments, you need to log in
How to search for information in a SQL database in a JSON format column?
Good day!
Please help me solve the problem. There are two columns in the DB (MySQL version: 5.7.26-29):
1 column
{
"1": {
"age_to": "8",
"gender": "children",
"age_from": "4",
"quantity": "400",
"additionally": "no"
},
"2": {
"age_to": "13",
"gender": "men",
"age_from": "9",
"quantity": "600",
"additionally": "no"
},
"3": {
"age_to": "70",
"gender": "men",
"age_from": "14",
"quantity": "900",
"additionally": "no"
},
"4": {
"age_to": "13",
"gender": "woman",
"age_from": "9",
"quantity": "600",
"additionally": "yes"
}
}
[
{
"value": "Боль в животе",
"color": "red",
},
{
"value": "Боль в костях",
"color": "blue",
}
]
Answer the question
In order to leave comments, you need to log in
https://dev.mysql.com/doc/refman/5.7/en/json.html
https://dev.mysql.com/doc/refman/8.0/en/json.html
You can work with JSON normally in MySQL, you just need to choose data structures suitable for searching, and not like in the 1st column (associative array).
Each such object in the 1st column
{
"age_to": "8",
"gender": "children",
"age_from": "4",
"quantity": "400",
"additionally": "no"
}
must be stored in a separate record so that they can be searched. SELECT * FROM table WHERE column ->> '$.a.b' = 'bla-bla'
SEARCH: Bone painThis is a full text search.
Never store such data in json format.
Store in the normal form, choose the usual where
Thank you all for your advice, probably really need to redo the database structure.
But there are a number of questions:
1) If there are two tables, the query, as far as I understand, is this:
$row = $db->super_query( "SELECT * FROM name_table p LEFT JOIN name_table_two n ON (p.id=n.parent_id) WHERE........
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question