MySQL中LIKE子句相关使用的学习教程_MySQL
MySQL LIKE 语法
LIKE 运算符用于 WHERE 表达式中,以搜索匹配字段中的指定内容,语法如下:
WHERE column LIKE pattern WHERE column NOT LIKE pattern
在 LIKE 前面加上 NOT 运算符时,表示与 LIKE 相反的意思,即选择 column 不包含 pattern 的数据记录。
LIKE 通常与通配符 % 一起使用,% 表示通配 pattern 中未出现的内容。而不加通配符 % 的 LIKE 语法,表示精确匹配,其实际效果等同于 = 等于运算符。
LIKE 使用实例
下面是一个使用 LIKE 查询数据的例子:
user 用户表原始数据:
SELECT * FROM user WHERE username LIKE '小%'
返回查询结果如下:
该例子是找出所有 username 以“小” 开头的记录,小% 表示以“小”字符开头,而后面可以是任意字符。同样, %小 表示以“小”结尾,而 %小%则表示包含“小”这个字符(并一同包括 '%小' 与 '小%' 这两种情况)。
下面这个例子,将查询出所有 username 中字段任意位置包含 a 字符的记录:
SELECT * FROM user WHERE username LIKE '%a%'
MySQL LIKE 大小写
MySQL LIKE 匹配字符时,默认是不区分大小写的,如果需要在匹配的时候区分大小写,可以加入 BINARY 操作符:
SELECT * FROM user WHERE username LIKE BINARY '%azz%' SELECT * FROM user WHERE username LIKE BINARY '%aZZ%'
BINARY 操作符表示按照二进制进行比较,因此加上该操作符后,便可以严格区分大小写,因此以上两条 SQL 查询出来的内容是不同的。
MySQL LIKE 中文字符匹配
由于数据存储编码问题,在某些情况下,MySQL 进行 LIKE 搜索返回的数据中除了符合要求的数据外,往往还会返回许多不相干的数据。这时候也需要在 LIKE 后面加上 BINARY 操作符以进行二进制比较:
SELECT * FROM user WHERE username LIKE BINARY '%小%'
提示
当在 LIKE 匹配时加上 BINARY 操作符后,则会严格区分英文大小写。因此当检索的内容是中英文混合且需要忽略英文大小写的时候,就会遇到麻烦。为解决此问题,需要引入 MySQL 中的 UPPER() 与 CONCAT() 函数:
UPPER():将英文字符串变大写,同UCASE()
CONCAT():将多个字符串连接成一个字符串
语法如下:
UPPER(str) CONCAT(str1,str2,...)
因此当我们要进行中英文混合匹配检索且要忽略英文大小写时,可以使用如下例所示的 SQL 语句:
SELECT * FROM username WHERE UPPER(username) LIKE BINARY CONCATt('%',UPPER('a中文b'),'%')
LIKE 运算符的效率
LIKE 运算符要对字段数据进行逐一扫描匹配,实际执行的效率是较差的,哪怕该字段已经建有索引(a% 这种方式会用到索引)。当数据量较大时,要尽可能的减少 LIKE 运算符的使用,也没有太多优化的余地。
在PHP脚本使用LIKE子句
可以使用WHERE ... LIKE子句类似的语法在PHP 的 mysql_query() 函数。此函数用于执行SQL命令,紧接着另一个PHP mysql_fetch_array()函数可用于获取所有选定的数据,如果WHERE ... LIKE子句连同SELECT命令一起使用。
但是,如果WHERE ... LIKE子句正在连同DELETE 或UPDATE命令使用,PHP函数不再是必须的。
示例
试试下面的例子,tutorials_tbl表所有记录其作者姓名包含jay将被返回:
<?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT tutorial_id, tutorial_title, tutorial_author, submission_date FROM tutorials_tbl WHERE tutorial_author LIKE "%jay%"'; mysql_select_db('test'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { echo "Tutorial ID :{$row['tutorial_id']} <br> ". "Title: {$row['tutorial_title']} <br> ". "Author: {$row['tutorial_author']} <br> ". "Submission Date : {$row['submission_date']} <br> ". "--------------------------------<br>"; } echo "Fetched data successfully\n"; mysql_close($conn); ?>
以上就是MySQL中LIKE子句相关使用的学习教程_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!

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.
