MySQL中文模糊检索问题解决方法_MySQL
在 MySQL下,在进行中文模糊检索时,经常会返回一些与之不相关的记录,如查找 "%a%" 时,返回的可能有中文字符,却没有a字符存在。本人以前也曾遇到过类似问题,经详细阅读MySQL的Manual,发现可以有一种方法很方便的解决并得到满意的结果。
例子:
希望通过“标题”对新闻库进行检索,关键字可能包含是中英文,如下SQL语句:
QUOTE:
select id,title,name from achech_com.news where title like '%a%'
返回的结果,某些title字段确定带了“a”关键字,而有些则只有中文,但也随之返回在检索结果中。
解决方法,使用 BINARY 属性进行检索,如:
QUOTE:
select id,title,name from achech_com.news where binary title like '%a%'
返回的结果较之前正确,但英文字母区分大小写,故有时在检索如“Achech”及“achech”的结果是不一样的。知道了使用 BINARY 属性可以解决前面这个问题,再看看 MySQL 支持的UCASE 及 CONCAT 函数,其中 UCASE 是将英文全部转成大写,而CONCAT函数的作用是对字符进行连接,以下是我们完全解决后的SQL 语句:
QUOTE:
select id,title,name from achech_com.news
where binary ucase(title) like concat('%',ucase('a'),'%')
检索的步骤是先将属性指定为 BINARY ,以精确检索结果,而被 like 的 title内容存在大小写字母的可能,故先使用 ucase 函数将字段内容全部转换成大写字母,然后再进行 like 操作,而 like 的操作使用模糊方法,使用 concat的好处是传进来的可以是直接的关键字,不需要带“%”万用符,将“'a'”直接换成你的变量,在任何语言下都万事无忧了。 当然你也可以这么写:
QUOTE:
select id,title,name from achech_com.news where binary ucase(title) like ucase('%a%')
检索的结果还算满意吧,不过速度可能会因此而慢N毫秒。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Pinduoduo software provides a lot of good products, you can buy them anytime and anywhere, and the quality of each product is strictly controlled, every product is genuine, and there are many preferential shopping discounts, allowing everyone to shop online Simply can not stop. Enter your mobile phone number to log in online, add multiple delivery addresses and contact information online, and check the latest logistics trends at any time. Product sections of different categories are open, search and swipe up and down to purchase and place orders, and experience convenience without leaving home. With the online shopping service, you can also view all purchase records, including the goods you have purchased, and receive dozens of shopping red envelopes and coupons for free. Now the editor has provided Pinduoduo users with a detailed online way to view purchased product records. method. 1. Open your phone and click on the Pinduoduo icon.

Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

Excel spreadsheet is one of the office software that many people are using now. Some users, because their computer is Win11 system, so the English interface is displayed. They want to switch to the Chinese interface, but they don’t know how to operate it. To solve this problem, this issue The editor is here to answer the questions for all users. Let’s take a look at the content shared in today’s software tutorial. Tutorial for switching Excel to Chinese: 1. Enter the software and click the "File" option on the left side of the toolbar at the top of the page. 2. Select "options" from the options given below. 3. After entering the new interface, click the “language” option on the left

How to display Chinese characters correctly in PHPDompdf When using PHPDompdf to generate PDF files, it is a common challenge to encounter the problem of garbled Chinese characters. This is because the font library used by Dompdf by default does not contain Chinese character sets. In order to display Chinese characters correctly, we need to manually set the font of Dompdf and make sure to select a font that supports Chinese characters. Here are some specific steps and code examples to solve this problem: Step 1: Download the Chinese font file First, we need

"WWE2K24" is a racing sports game created by Visual Concepts and was officially released on March 9, 2024. This game has been highly praised, and many players are eagerly interested in whether it will have a Chinese version. Unfortunately, so far, "WWE2K24" has not yet launched a Chinese language version. Will wwe2k24 be in Chinese? Answer: Chinese is not currently supported. The standard version of WWE2K24 in the Steam Chinese region is priced at 199 yuan, the deluxe version is 329 yuan, and the commemorative edition is 395 yuan. The game has relatively high configuration requirements, and there are certain standards in terms of processor, graphics card, or running memory. Official recommended configuration and minimum configuration introduction:

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

Title: An effective way to repair Chinese garbled characters in PHPDompdf. When using PHPDompdf to generate PDF documents, garbled Chinese characters are a common problem. This problem usually stems from the fact that Dompdf does not support Chinese character sets by default, resulting in Chinese content not being displayed correctly. In order to solve this problem, we need to take some effective ways to fix the Chinese garbled problem of PHPDompdf. 1. Use custom font files. An effective way to solve the problem of Chinese garbled characters in Dompdf is to use

Some friends may accidentally set it to English when installing the system. As a result, all the interfaces are changed to English and they cannot be understood. In fact, we can set the language in the control panel and change the language to Chinese. Let’s take a look at how to change it. How to change the language in win7 to Chinese 1. First click the button in the lower left corner of the screen, and then select "Control Panel" 2. Find "Changedispalylanguage" under "Clock, Language, and Region" 3. Click "English" below to select from the drop-down menu Simplified Chinese. 4. After confirmation, click "Logoffnow" to log out and restart the computer. 5. After coming back
