How to query json in the database in mysql5.6 and below

How to query json in the database in mysql5.6 and below

When saving data in MySQL, sometimes some messy and infrequently used data is thrown into a json field

Let's talk about the formats that MySQL should pay attention to when storing JSON:

1: Be careful not to transcode the Chinese text when saving. Transcoding will make the query very troublesome. When compressing, add an extra parameter at the end, which will make it more convenient!

json_encode(array(),JSON_UNESCAPED_UNICODE);

Advantages: Chinese characters are better matched when searching in this way

2: Unify the fields. It is best to determine the field names when saving. It is impossible for one person to develop a larger project. Unifying the fields can reduce a lot of unnecessary troubles and bugs caused by disagreements on fields (I have suffered a loss on this, but I didn’t think about it when I led everyone to do the project, and the rush caused the subsequent testing and modification to take more time than the development)

Benefits: Reduce the amount of code processing for query data and reduce project page display bugs

3: Never store a two-dimensional array if you can store a one-dimensional array

Reason: Two-dimensional arrays are uncontrollable. This is still more based on demand.

Personal habit. This is my habit anyway, hahaha. If you are unhappy, you can hit me!

Let's get down to business: of course, the query uses like

For example, there is a content field in a table. Now we need to find the content field whose actid is 123456789.

5.7: select * from table where content->'$.actid' like '%123456789%';
5.6: select * from table where content like '%"actid":"123456789"%'

This should be understandable at a glance. Teaching is definitely a full set, and it is also a health care. So some people in the code say how to use it

$id="123456789";
$sql="select * from table where content like '%\"actid":\""$id"\"%\'";

The above is what I introduced to you about how to query JSON in the database in MySQL 5.6 and below. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

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?
  • Two query methods when the MySQL query field type is json
  • Python query mysql, return json instance
  • Mysql directly queries the data in the stored Json string

<<:  Vue uses echarts to draw an organizational chart

>>:  Example of how to create and run multiple MySQL containers in Docker

Recommend

Vue + element dynamic multiple headers and dynamic slots

Table of contents 1. Demand 2. Effect 3. All code...

A detailed explanation of how React Fiber works

Table of contents What is React Fiber? Why React ...

Detailed tutorial on installing mysql under Linux

1. Shut down the mysql service # service mysqld s...

How to fix the WeChat applet input jitter problem

Find the problem Let's look at the problem fi...

Introduction to the use of MySQL performance stress benchmark tool sysbench

Table of contents 1. Introduction to sysbench #Pr...

Detailed explanation of Nginx process scheduling problem

Nginx uses a fixed number of multi-process models...

The implementation process of Linux process network traffic statistics

Preface Linux has corresponding open source tools...

Webservice remote debugging and timeout operation principle analysis

WebService Remote Debugging In .NET, the remote d...

Several techniques for playing sounds with CSS

CSS is the realm of style, layout, and presentati...

Code to enable IE8 in IE7 compatibility mode

The most popular tag is IE8 Browser vendors are sc...

The final solution to Chrome's minimum font size limit of 12px

I believe that many users who make websites will ...

How to use union all in MySQL to get the union sort

Sometimes in a project, due to some irreversible ...

Detailed analysis and usage of tcpdump command under Linux

Introduction To put it simply, tcpdump is a packe...