I now have a data table with the following fields: ID, home, away, status。
JSON data is stored in the home and away fields
| id | home | away | status |
|---|---|---|---|
| 1 | {"id":10, "value":0} |
{"id":11, "value":0} |
3 |
| 2 | {"id":11, "value":0} |
{"id":10, "value":0} |
3 |
| 3 | {"id":10, "value":0} |
{"id":20, "value":0} |
3 |
| 4 | {"id":40, "value":0} |
{"id":10, "value":0} |
2 |
Now I can use the following statement to query the expected data.
If I want to filter out the data with ID 11 in home and away during query, how can I modify the SQL?
SELECT
*
FROM
table_name
WHERE
status = '3'
AND JSON_EXTRACT( home, '$.id' ) = 10
OR JSON_EXTRACT( away, '$.id' ) = 10
ORDER BY
id DESC
LIMIT 10
Expected data:
| id | home | away | status |
|---|---|---|---|
| 3 | {"id":10, "value":0} |
{"id":20, "value":0} |
3 |
status_id, because it'sstatus