当前位置:网站首页>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);
?>边栏推荐
- Introduction of functions in C language (blood Book 20000 words!!!)
- 看完这12个面试问题,新媒体运营岗位就是你的了
- Let me teach you how to assemble a registration center?
- Slice function of JS handwriting function (thoroughly understand the header but not the footer)
- 我来教你如何组装一个注册中心?
- Bash shell interaction free
- Image batch processing | necessary skills
- Linux initializes MySQL with fatal error: could not find my-default.cnf
- Day112. Shangyitong: Mobile verification code login function
- 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}
猜你喜欢

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

Chapter 2-14 sum integer segments

Different HR labels

Path and attribute labels of picture labels

Alibaba technology has four sides + intersection +hr, and successfully got the offer. Can't double non undergraduate students enter the big factory?

HCIP第九天_BGP实验

台大林轩田《机器学习基石》习题解答和代码实现 | 【你值得拥有】

1w5字详细介绍分布式系统的那些技术方案

You're not still using xshell, are you? This open source terminal tool is yyds!
随机推荐
Machine learning how to achieve epidemic visualization -- epidemic data analysis and prediction practice
C #, introductory tutorial -- debugging skills and logical error probe technology and source code when the program is running
第2章-2 计算分段函数[1]
置顶各大平台,22版面试核心知识解析笔记,强势上榜
Go panic and recover
Smartbi of smart smart smart software completed the c-round financing and accelerated the domestic Bi into the intelligent era
Top all major platforms, 22 versions of interview core knowledge analysis notes, strong on the list
Business digitalization is running rapidly, and management digitalization urgently needs to start
Mobaxtermsession synchronization
DIY system home page, your personalized needs PRO system to meet!
Vk1620 temperature controller / smart meter LED digital display driver chip 3/4-wire interface with built-in RC oscillator to provide technical support
分布式系统架构理论与组件
Slice function of JS handwriting function (thoroughly understand the header but not the footer)
HCIP第八天
PostgreSQL: cannot change the type of column used by a view or rule
Hundreds of billions of it operation and maintenance market has come to the era of speaking by "effect"
HCIP第九天_BGP实验
Solution: indexerror: index 13 is out of bounds for dimension 0 with size 13
Use of tkmapper - super detailed
Kubernetes cluster configuration serviceaccount