user10384418
user10384418

Reputation: 89

How To check If mysql DB column has json array of object and contains specific string

I have saved the json data in mysql database column which looks like below

[{"one":"mg road plot number 711 xyz"},{"two":"dc road plot number 711 xyz"}]

json data like above is being stored for all the records with different address. So what I am trying to do is , I want to search all the customer who are having address of mg road.

So I have tried JSON_SEARCH and JSON_CONTAINS but it always returning me no records found even though there are 2 records in a table. So where I am going wrong.

SELECT * FROM `tbl_clients_data` WHERE JSON_SEARCH(reci_address,'all', "mg road")


SELECT * FROM `tbl_clients_data` WHERE JSON_CONTAINS(reci_address, 'mg road', '$.one')

Upvotes: 2

Views: 1439

Answers (1)

Prabhjot Singh Kainth
Prabhjot Singh Kainth

Reputation: 1861

Just try this code:

SELECT * FROM `tbl_clients_data` WHERE JSON_EXTRACT(reci_address, '$[*].one') 
LIKE 'mg road%' OR JSON_EXTRACT(reci_address, '$[*].two') LIKE 'mg road%'

JSON_EXTRACT Extracts data from a JSON document. The extracted data is selected from the parts matching the path arguments. Returns all matched values; either as a single matched value, or, if the arguments could return multiple values, a result autowrapped as an array in the matching order.

Upvotes: 1

Related Questions