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

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

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
