当前位置:网站首页>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);
?>边栏推荐
- Eight ways to solve EMC and EMI conducted interference
- 49 opencv deep analysis profile
- KEGG通路的从属/注释信息如何获取
- 台大林轩田《机器学习基石》习题解答和代码实现 | 【你值得拥有】
- Smartbi of smart smart smart software completed the c-round financing and accelerated the domestic Bi into the intelligent era
- [opencv] generate transparent PNG image
- Recruiting talents, gbase high-end talent recruitment in progress
- Two dimensional array and operation
- Kubernetes cluster configuration serviceaccount
- 快速搭建一个网关服务,动态路由、鉴权的流程,看完秒会(含流程图)
猜你喜欢

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

TXT文本文件存储

Review the past and know the new MySQL isolation level

View the dimensions of the list

Div tags and span Tags

【单细胞高级绘图】07.KEGG富集结果展示

Day112. Shangyitong: Mobile verification code login function

我来教你如何组装一个注册中心?

C #, introductory tutorial -- debugging skills and logical error probe technology and source code when the program is running

Warehouse of multiple backbone versions of yolov5
随机推荐
[cloud computing] several mistakes that enterprises need to avoid after going to the cloud
Smart software completed round C financing, making Bi truly "inclusive"
Post it notes -- 45 {packaging of the uniapp component picker, for data transmission and processing -- Based on the from custom packaging that will be released later}
Machine learning how to achieve epidemic visualization -- epidemic data analysis and prediction practice
Does gbase 8s support storing relational data and object-oriented data?
Use of tkmapper - super detailed
Detailed explanation of DHCP distribution address of routing / layer 3 switch [Huawei ENSP]
Eight ways to solve EMC and EMI conducted interference
Larkapi access credentials overview
Deployment of kubernetes
JSON file storage
Vrrp+mstp configuration details [Huawei ENSP experiment]
Explain cache consistency and memory barrier
478-82(56、128、718、129)
C #, introductory tutorial -- debugging skills and logical error probe technology and source code when the program is running
Kubernetes cluster configuration DNS Service
Alibaba technology has four sides + intersection +hr, and successfully got the offer. Can't double non undergraduate students enter the big factory?
Quickly build a gateway service, dynamic routing and authentication process, and watch the second meeting (including the flow chart)
Go panic and recover
Chapter 2-14 sum integer segments