Detailed explanation of querying JSON format fields in MySQL

Detailed explanation of querying JSON format fields in MySQL

During the work development process, a requirement was encountered: the customer's name, mobile phone number, ID card, and document type needed to be dynamically stored. This means that the front end may transmit the information of one, two, or three people, which is a dynamic and unfixed number of four fields (name, mobile phone number, ID card, document type).

The front-end page is as follows:

This is the front end picture

I am using List To receive the json from the front end, mysql uses varchar to save this json array

[{
	"cardId": "110101199003072316",
	"cstName": "Zhang Shuang'er 1",
	"cstMobile": "13263654144",
	"idCardType": "1"
}, {
	"cardId": "11010119900307571X",
	"cstName": "Zhang Shuang'er 2",
	"cstMobile": "13263654144",
	"idCardType": "1"
}]

The product requirement is to fuzzy query these joint customer information. At the beginning, I wrote it wrongly:

select * from signCustomerGroup like '%儿%'

But later I found a problem. For example, if I fuzzily input the letter c, the English field name 'cardId' on the left would be matched.
Later I learned that MySQL 5.7 and later versions added the json type, and some json type functions can be used to directly query a field in json format.

The correct syntax is as follows:

Table fields:

id sign_customer_info_ext
1 [{“cstName”:“hhjk”,“cstMobile”:“14258669888”,“idCardType”:“1”,“cardId”:“460101199601012516”}]
2 [{"cstName":"ghhj中文1355","cstMobile":"18253558608","idCardType":"1","cardId":"460101199601012815"},{"cstName":"fhjj重要133366","cstMobile":"15555555555","idCardType":"1","cardId":"460101199601012313"}]

The main SQL function used is json_extract(), which is used to find all specified data from JSON format

1. json array query

Fuzzy query a field in the json array format:
Directions:
SELECT * FROM table name WHERE json_extract(field name,"$[*].json中key") like '%value to be searched%';
Examples:
SELECT * FROM table WHERE json_extract(sign_customer_info_ext,"$[*].cstName") like '%h%';
Precise query (Note: Precise query must specify the subscript of the array to which the queried field belongs, for example, if the first one is [0], the second one is [1])
SELECT id,sign_customer_info_ext FROM table WHERE json_extract(sign_customer_info_ext,"$[0].cstName") = 'ghhj中文1355';

2. Single json query

Single json parameter in frontend and mysql database:

{
	"cstName": "Ma Yun",
	"cstMobile": "17879767646",
	"idCardType": "1",
	"cardId": "E4813980"
}
Fuzzy query single json query:
Directions:
SELECT id,sign_customer_info_ext FROM table name WHERE json_extract(field name,"$.json中key") like '%马云%';
Examples:
SELECT id,sign_customer_info_ext FROM table WHERE json_extract(sign_customer_info_ext,"$.cstName") like '%Ma Yun%';

Summarize

This is the end of this article about querying json format in MySQL. For more relevant MySQL query json format field content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL implementation of extracting and querying JSON type field data
  • MySQL retrieves data based on the JSON field content as a query condition (including JSON arrays)
  • Mybatis mysql fuzzy query method (CONCAT multiple fields) and bug
  • Sample code for implementing string split function in MySQL query field
  • How to assign default values ​​to fields when querying MySQL
  • Querying MySQL date and time fields

<<:  HTML tbody usage

>>:  Pure js to achieve the effect of carousel

Recommend

How to solve the slow speed of MySQL Like fuzzy query

Question: Although the index has been created, wh...

Vue implements multi-grid input box on mobile terminal

Recently, the company has put forward a requireme...

Vue uses mockjs to generate simulated data case details

Table of contents Install mockjs in your project ...

Detailed explanation of MySQL master-slave replication and read-write separation

Article mind map Why use master-slave replication...

Detailed explanation of FTP environment configuration solution (vsftpd)

1. Install vsftpd component Installation command:...

mysql5.7.22 download process diagram

1. Go to the official website www.mysql.com and s...

Database query which object contains which field method statement

The database queries which object contains which ...

Understanding MySQL index pushdown in five minutes

Table of contents What is index pushdown? The pri...

Detailed tutorial for installing mysql5.7.21 under Windows system

MySQL Installer provides an easy-to-use, wizard-b...

SQL Aggregation, Grouping, and Sorting

Table of contents 1. Aggregate Query 1. COUNT fun...

MySQL 5.7.17 winx64 installation and configuration graphic tutorial

I summarized the previous notes on installing MyS...

How to execute Linux shell commands in Docker

To execute a shell command in Docker, you need to...

Linux uses stty to display and modify terminal line settings

Sttty is a common command for changing and printi...

Examples of clearfix and clear

This article mainly explains how to use clearfix a...

Detailed explanation of Vuex overall case

Table of contents 1. Introduction 2. Advantages 3...