Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query with a where clause in mysql json table

Tags:

json

mysql

I am using mysql 5.7.x

I can create a mysql json table

 CREATE TABLE t1 (jdoc JSON);

I can insert rows.

INSERT INTO t1 VALUES('{"key1": "value1", "key2": "value2"}');
INSERT INTO t1 VALUES('{"key1": "value11", "key2": "value22"}');

I can also get all rows:

SELECT * from t1;

How do I use a where clause?

select * from t1 where "key1" = "value1"
like image 600
Tampa Avatar asked Aug 24 '15 17:08

Tampa


1 Answers

You can try:

SELECT *
FROM t1
WHERE json_extract(jdoc, '$.key1')='value1'
like image 163
Sterls Avatar answered Sep 28 '22 05:09

Sterls