Find matching integers in JSON.
P粉252116587
P粉252116587 2023-07-25 08:47:02
0
1
437
<p>I want to find 123 in a JSON string in MySQL. </p><p>This is my query:</p><p><br /></p> <pre class="brush:php;toolbar:false;">SELECT * FROM `reports` WHERE JSON_CONTAINS(json_data, 123, "$.respCode");</pre> <p>When I run this code I get the following error: </p> <blockquote> <pre class="brush:php;toolbar:false;">Error Code: 3146. Invalid data type for JSON data in argument 1 to function json_contains; a JSON string or JSON type is required. 0.020 sec</pre> </blockquote> <p>My json looks like this in the json_data column: </p> <pre class="lang-json prettyprint-override"><code>{ "bla":"", "bla2":"", "bla3":"", "bla4":"", "respCode ":"123", "bla5":"" } </code></pre> <p>I also tried this</p> <p><code>WHERE JSON_UNQUOTE(JSON_EXTRACT('json_data', "$.respCode")) = '123'</code></p>
P粉252116587
P粉252116587

reply all(1)
P粉617597173

This is the syntax of the MySQL JSON_CONTAINS() function:

JSON_CONTAINS(target_json, candidate_json)
JSON_CONTAINS(target_json, candidate_json, path)

You can try

SELECT * FROM `reports` 
WHERE JSON_CONTAINS(json_data, '{"respCode":"123"}');

DB Fiddle Demo

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!