当前位置:网站首页>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);
?>边栏推荐
- 看得清比走得快更重要,因为走得对才能走得远
- How can MySQL query judge whether multiple field values exist at the same time
- Business digitalization is running rapidly, and management digitalization urgently needs to start
- Bash shell interaction free
- Mongodb (compare relational database, cloud database, common command line, tutorial)
- PostgreSQL queries [table field type] and [all series] in the library
- JSON 文件存储
- Vrrp+mstp configuration details [Huawei ENSP experiment]
- VS2015使用dumpbin 查看库的导出函数符号
- 站在大佬的肩膀上,你可以看的更远
猜你喜欢

TXT text file storage

CSV file storage
![[soft test software evaluator] 2013 comprehensive knowledge over the years](/img/c5/183acabd7015a5e515b7d83c127b2c.jpg)
[soft test software evaluator] 2013 comprehensive knowledge over the years

Mongodb (compare relational database, cloud database, common command line, tutorial)

Go synergy

C language array pointer and pointer array discrimination, analysis of memory leakage

Machine learning how to achieve epidemic visualization -- epidemic data analysis and prediction practice

Quickly build a gateway service, dynamic routing and authentication process, and watch the second meeting (including the flow chart)

Basic syntax of jquey

阿里技术四面+交叉面+HR面,成功拿到offer,双非本科进不了大厂?
随机推荐
Detailed explanation of switch link aggregation [Huawei ENSP]
[opencv] generate transparent PNG image
Argocd Web UI loading is slow? A trick to teach you to solve
(13) Simple temperature alarm device based on 51 single chip microcomputer
Kubernetes technology and Architecture (VII)
Eight ways to solve EMC and EMI conducted interference
Digital signatures and Ca certificates
站在大佬的肩膀上,你可以看的更远
Alibaba internal interview materials
Go channel
Will sqlserver CDC 2.2 generate table locks when extracting large tables from the source
When will brain like intelligence, which is popular in academia, land? Let's listen to what the industry masters say - qubits, colliders, x-knowledge Technology
看得清比走得快更重要,因为走得对才能走得远
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}
Review the past and know the new MySQL isolation level
看完这12个面试问题,新媒体运营岗位就是你的了
Go panic and recover
CSV文件存储
PHP Basics - PHP uses mysqli
Overview of head pose estimation