Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query JSON array in MYSQL with AND

Tags:

json

mysql

If i have json like the following in a column in a mysql database

[
 {
  "name": "John",
  "checked": true
 },
 {
  "name": "Lucy",
  "checked": false
 }
]

how can I select in mysql all rows where in the same object name = 'John' and checked = true.

The json objects may have more keys and keys may not be in a specific order.

like image 668
ykay says Reinstate Monica Avatar asked Nov 23 '25 08:11

ykay says Reinstate Monica


1 Answers

Just use JSON_CONTAINS:

SELECT * from `users`
WHERE JSON_CONTAINS(`data`, '{"name": "John","checked": true}');
like image 170
Alexandr Kalashnikov Avatar answered Nov 24 '25 23:11

Alexandr Kalashnikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!