PrefaceRecently, I encountered such a requirement in the project: I need to retrieve a result set containing specified content in the data table. The data type of this field is text, and the stored content is in json format. The specific table structure is as follows: CREATE TABLE `product` ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID', `name` VARCHAR(100) NOT NULL DEFAULT '' COMMENT 'Product name' COLLATE 'utf8mb4_general_ci', `price` DECIMAL(10,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT 'Product price', `suit` TEXT NOT NULL COMMENT 'Applicable store json format to save the store ID' COLLATE 'utf8mb4_general_ci', `status` TINYINT(3) NOT NULL DEFAULT '0' COMMENT 'Status 1-Normal 0-Deleted 2-Delisted', `create_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Release time', `update_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'modification time', PRIMARY KEY (`id`) USING BTREE ) COMMENT='Product Table' COLLATE='utf8mb4_general_ci' ENGINE=InnoDB AUTO_INCREMENT=1 ; The table data is as follows: Current requirement: Find the data containing 10001 in suit->hotel. Through Google and Baidu search, we can find the following solutions: Option 1:select * from product where suit like '%"10001"%'; #Like method cannot use indexes, has poor performance, and lacks accuracy Option 2:select * from product where suit LOCATE('"10001"', 'suit') > 0; # The LOCATE method has the same problem as the LIKE method Option 3:select * from product where suit != '' and json_contains('suit'->'$.hotel', '"10001"'); #Search with MySQL built-in json function, which requires MySQL 5.7 or above to support, with high accuracy, and cannot use full-text index Option 4 (finally adopted option):select * from product where MATCH(suit) AGAINST('+"10001"' IN BOOLEAN MODE); #You can use full-text indexing. The default limit for MySQL keywords is at least 4 characters. You can modify ft_min_word_len=2 in mysql.ini to take effect after restarting. For more usage of MATCH() AGAINST(), see the MySQL Reference Manual: https://dev.mysql.com/doc/refman/5.6/ja/fulltext-boolean.html SummarizeThis is the end of this article about MySql fuzzy query json keyword retrieval solution example. For more relevant MySql json keyword retrieval 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:
|
<<: HTML image img tag_Powernode Java Academy
>>: JavaScript immediate execution function usage analysis
Table of contents Vue first screen performance op...
Table of contents Simple CASEWHEN function: This ...
1. Unzip the file to the current directory Comman...
Preface Fix the footer area at the bottom. No mat...
This article analyzes the process of shutting dow...
When we write code, we often need to know the dif...
Table of contents 1 redis configuration file 2 Do...
The replace statement is generally similar to ins...
Implementation of regular backup of Mysql databas...
Table of contents 1. Problem scenario 2. Cause An...
Table of contents Preface Install vue-i18n Config...
Preface I have an old laptop with Win7. In order ...
The first step is to add the corresponding databa...
Here is an introduction to changing the password ...
1. Installation Environment Computer model: Lenov...