当前位置:网站首页>10、学习MySQL LIKE 子句
10、学习MySQL LIKE 子句
2022-07-28 07:58:00 【微服务 spring cloud】
MySQL LIKE 子句
我们知道在 MySQL 中使用 SQL SELECT 命令来读取数据, 同时我们可以在 SELECT 语句中使用 WHERE 子句来获取指定的记录。
WHERE 子句中可以使用等号 = 来设定获取数据的条件,如 "xxxxxx_author = 'XXXXXX.COM'"。
但是有时候我们需要获取 xxxxxx_author 字段含有 "COM" 字符的所有记录,这时我们就需要在 WHERE 子句中使用 SQL LIKE 子句。
SQL LIKE 子句中使用百分号 %字符来表示任意字符,类似于UNIX或正则表达式中的星号 *。
如果没有使用百分号 %, LIKE 子句与等号 = 的效果是一样的。
语法
以下是 SQL SELECT 语句使用 LIKE 子句从数据表中读取数据的通用语法:
SELECT field1, field2,...fieldN
FROM table_name
WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'- 你可以在 WHERE 子句中指定任何条件。
- 你可以在 WHERE 子句中使用LIKE子句。
- 你可以使用LIKE子句代替等号 =。
- LIKE 通常与 % 一同使用,类似于一个元字符的搜索。
- 你可以使用 AND 或者 OR 指定一个或多个条件。
- 你可以在 DELETE 或 UPDATE 命令中使用 WHERE...LIKE 子句来指定条件。
在命令提示符中使用 LIKE 子句
以下我们将在 SQL SELECT 命令中使用 WHERE...LIKE 子句来从MySQL数据表 xxxxxx_tbl 中读取数据。
实例
以下是我们将 xxxxxx_tbl 表中获取 xxxxxx_author 字段中以 COM 为结尾的的所有记录:
SQL LIKE 语句:
mysql> use XXXXXX;
Database changed
mysql> SELECT * from xxxxxx_tbl WHERE xxxxxx_author LIKE '%COM';
+-----------+---------------+---------------+-----------------+
| xxxxxx_id | xxxxxx_title | xxxxxx_author | submission_date |
+-----------+---------------+---------------+-----------------+
| 3 | 学习 Java | XXXXXX.COM | 2021-11-20 |
| 4 | 学习 Python | XXXXXX.COM | 2021-11-21 |
+-----------+---------------+---------------+-----------------+
2 rows in set (0.01 sec)在PHP脚本中使用 LIKE 子句
你可以使用PHP函数的 mysqli_query() 及相同的 SQL SELECT 带上 WHERE...LIKE 子句的命令来获取数据。
该函数用于执行 SQL 命令,然后通过 PHP 函数 mysqli_fetch_array() 来输出所有查询的数据。
但是如果是 DELETE 或者 UPDATE 中使用 WHERE...LIKE 子句的S QL 语句,则无需使用 mysqli_fetch_array() 函数。
实例
以下是我们使用PHP脚本在 xxxxxx_tbl 表中读取 xxxxxx_author 字段中以 COM 为结尾的的所有记录:
MySQL LIKE 子句测试:
<?php
$dbhost = 'localhost'; // mysql服务器主机地址
$dbuser = 'root'; // mysql用户名
$dbpass = '123456'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('连接失败: ' . mysqli_error($conn));
}
// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");
$sql = 'SELECT xxxxxx_id, xxxxxx_title,
xxxxxx_author, submission_date
FROM xxxxxx_tbl
WHERE xxxxxx_author LIKE "%COM"';
mysqli_select_db( $conn, 'XXXXXX' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
die('无法读取数据: ' . mysqli_error($conn));
}
echo '<h2>xx教程 mysqli_fetch_array 测试<h2>';
echo '<table border="1"><tr><td>教程 ID</td><td>标题</td><td>作者</td><td>提交日期</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
echo "<tr><td> {$row['xxxxxx_id']}</td> ".
"<td>{$row['xxxxxx_title']} </td> ".
"<td>{$row['xxxxxx_author']} </td> ".
"<td>{$row['submission_date']} </td> ".
"</tr>";
}
echo '</table>';
mysqli_close($conn);
?>边栏推荐
- Use of tkmapper - super detailed
- How to obtain the subordinate / annotation information of KEGG channel
- Digital signatures and Ca certificates
- MDM data quality application description
- Solution: indexerror: index 13 is out of bounds for dimension 0 with size 13
- JSON 文件存储
- Introduction of functions in C language (blood Book 20000 words!!!)
- (IROS 2022) 基于事件相机的单目视觉惯性里程计 / Event-based Monocular Visual Inertial Odometry
- Eight ways to solve EMC and EMI conducted interference
- XMIND Zen installation tutorial
猜你喜欢

Principle of line of sight tracking and explanation of the paper

Go synergy
![Detailed explanation of switch link aggregation [Huawei ENSP]](/img/c2/f9797fe8b17a418466b60cc3dc50a1.png)
Detailed explanation of switch link aggregation [Huawei ENSP]

Let me teach you how to assemble a registration center?

XMIND Zen installation tutorial

Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (top)

图片批处理|必备小技能

Vk1620 temperature controller / smart meter LED digital display driver chip 3/4-wire interface with built-in RC oscillator to provide technical support

Completion report of communication software development and Application

Different HR labels
随机推荐
Kubernetes cluster configuration DNS Service
Feign调用异常[Running, pool size = 10, active threads = 10, queued tasks = 0, completed tasks = n]
Detailed explanation of DHCP distribution address of routing / layer 3 switch [Huawei ENSP]
There is a bug in installing CONDA environment
Go synergy
Bluetooth technology | it is reported that apple, meta and other manufacturers will promote new wearable devices, and Bluetooth will help the development of intelligent wearable devices
Analysis and recurrence of network security vulnerabilities
Top all major platforms, 22 versions of interview core knowledge analysis notes, strong on the list
Does gbase 8s support storing relational data and object-oriented data?
站在大佬的肩膀上,你可以看的更远
Distributed system architecture theory and components
Eight ways to solve EMC and EMI conducted interference
图片批处理|必备小技能
为什么 ThreadLocal 可以做到线程隔离?
Path and attribute labels of picture labels
What are the main uses of digital factory management system
Image batch processing | necessary skills
Detailed explanation of the basic use of express, body parse and express art template modules (use, route, path matching, response method, managed static files, official website)
49 opencv deep analysis profile
看得清比走得快更重要,因为走得对才能走得远