简体   繁体   中英

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')

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM