Two query methods when the MySQL query field type is json

Two query methods when the MySQL query field type is json

The table structure is as follows:

id varchar(32) 
info json

data:

id = 1
info = {"age": "18","disname":"Xiao Ming"}

--------------------------------------------

Now I need to get the value of disanme in info. The query method is:

1.

select t.id,JSON_EXTRACT(t.info,'$.disname') as disname from tableName t where 1=1

result:

id = 1, disname = "Xiao Ming"

The disname value found in the above SQL statement contains double quotes. Sometimes we don't need double quotes. In this case, we need to use the following method.

2.

select t.id,t.info ->> '$.disname' as disname from tableName t where 1=1

result:

id = 1 , disname = Xiao Ming

ps: Let's look at the mysql query json field

Create table statement

Create Table 
 CREATE TABLE `test` (
  `id` int(10) ,
  `user` json DEFAULT NULL COMMENT 'User information',
  PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8

Inserting Data

It should be noted that single quotes are required outside the JSON data to distinguish

INSERT INTO test (id,USER) VALUES(1,'{"name": "tom", "age": 18, "money": 3000}');
INSERT INTO test (id,USER) VALUES(2,'{"name": "jack", "age": 20, "money": 100}');
INSERT INTO test (id,USER) VALUES(3,'{"name": "tony", "age": 21, "money": 100}');
INSERT INTO test (id,USER) VALUES(4,'{"name": "danny", "age": 21, "money": 20}');
INSERT INTO test (id,USER) VALUES(5,'{"name": "janny", "age": 23, "money": 20}');

The table data is as follows

Query Statement

SELECT id,JSON_EXTRACT(USER,'$.name')FROM test;

Here are the query results

Summarize

The above are two query methods introduced by the editor when the MySQL query field type is json. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • MySql fuzzy query json keyword retrieval solution example
  • Example code for converting Mysql query result set into JSON data
  • Detailed explanation of querying JSON format fields in MySQL
  • MySQL json format data query operation
  • Summary of related functions for Mysql query JSON results
  • Which is faster among MySQL full-text index, joint index, like query, and json query?
  • Python query mysql, return json instance
  • How to query json in the database in mysql5.6 and below
  • Mysql directly queries the data in the stored Json string

<<:  How to customize an EventEmitter in node.js

>>:  How to use Docker to build a tomcat cluster using nginx (with pictures and text)

Recommend

js realizes horizontal and vertical sliders

Recently, when I was doing a practice project, I ...

Detailed explanation of MySQL InnoDB index extension

Index extension: InnoDB automatically extends eac...

WeChat Mini Program Lottery Number Generator

This article shares the specific code of the WeCh...

10 ways to view compressed file contents in Linux (summary)

Generally speaking, when we view the contents of ...

Detailed explanation of how to use join to optimize SQL in MySQL

0. Prepare relevant tables for the following test...

js to realize the rotation of web page pictures

This article shares the specific code of js to re...

Analysis of two usages of the a tag in HTML post request

Two examples of the use of the a tag in HTML post...

Win10 installation Linux system tutorial diagram

To install a virtual machine on a Windows system,...

Zookeeper unauthorized access test problem

Table of contents Preface Detect Zookeeper servic...

JS implements the dragging and placeholder functions of elements

This blog post is about a difficulty encountered ...

Summary of knowledge points about events module in Node.js

Through the study and application of Node, we kno...

Rounding operation of datetime field in MySQL

Table of contents Preface 1. Background 2. Simula...

Solve the cross-domain problem of Vue+SpringBoot+Shiro

Table of contents 1. Configure Vue front end 1. D...

Several solutions for CSS record text icon alignment

It is very common to see images and text displaye...

Use html-webpack-plugin' to generate HTML page plugin in memory

When we package the webpackjs file, we introduce ...