2

How can I find a value within a JSON array in MySQL? I am trying this option

https://sqlize.online/sql/mysql80/df842fcbe7d246262b65740c80668c4e/

create table tbl (id int, home json);

insert into tbl values 
(1, '[
    "1",
    "2",
    "3"
]');

select * 
from tbl
where json_contains(home, '1');

but there are no results.

1
  • You're missing a double quote ("") around 1. This should work: select * from tbl where json_contains(home, '"1"'); Commented Sep 22, 2023 at 12:49

1 Answer 1

1

You can do it as follows using JSON_CONTAINS :

select * 
from tbl
where JSON_CONTAINS(home, '"1"', '$');

PS : "1" is a string so you have to use double quotes.

This is an other way to do it using JSON_SEARCH :

select *
from tbl 
where JSON_SEARCH(home, 'one', "1") is not null
Sign up to request clarification or add additional context in comments.

4 Comments

Glad to hear I could help :)
Please tell me more, for some reason it does not work in PHP through mysqli_query.
Error: $result = $mysqli->query('SELECT * from products where JSON_SEARCH(json, "one", "361")');
You will need to escape double quotes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.