Solution: MySQL JSON field matching value ignores case-insensitive matching results

problem background

The character set of MySQL 5.7 database is utf8 due to historical reasons, and the collation is utf8_general_ci,

The table is newly created, the default character set is set to ut8mb4, the collation is ut8mb4_general_ci,

  • utf8_bin/utf8mb4: store each character in the string in hexadecimal, case-sensitive.
  • utf8_general_ci/utf8mb4_general_ci: case insensitive, ci is the abbreviation of case insensitive, that is, case insensitive.

The requirements are as follows:

  • request_param in the database t_test_json table is a jSON type field,

  • The value of user_name is: {"user_name":"ABC"}, the query match uses lowercase abc for matching, which needs to be hit.

  • Cannot be processed with like syntax.

Initially using this writing method, the matching result was found to be case-sensitive.

SELECT
	* 
FROM
	t_test_json 
WHERE
	request_param -> '$.user_name' = 'abc'

The database is ABC, and the query is matched with abc, which is a miss.

repair solution

  • Use -> syntax meaning:
    • The operator is used ->to extract the value of the request_param key in the JSON object and match it as a JSON object.
    • ->The operator returns a JSON object, not a string, so LIKE cannot be used directly for fuzzy matching.
  • use ->>grammatical meaning
    • The operator is used ->>to extract the value of the request_param key in the JSON object and fuzzy match it as a string.
    • ->>The operator returns a string, so LIKE can be used for fuzzy matching.

Therefore, the two query statements are different in syntax and in the way they are matched, and may result in different results.

The fix is ​​as follows:

SELECT
	* 
FROM
	t_test_json 
WHERE
	request_param ->> '$.user_name' = 'abc' COLLATE utf8mb4_general_ci 

The data inventory is ABC, and the query matching is matched with abc, and hits.

Guess you like

Origin blog.csdn.net/hadues/article/details/130895617