当前位置:网站首页>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);
?>边栏推荐
- Ciou loss
- Recycling of classes loaded by classloader
- Do you know the five minute rule and the ten byte rule?
- Path and attribute labels of picture labels
- Data fabric, next air outlet?
- 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
- Kubernetes data persistence scheme
- Uniapp ---- detailed steps to obtain the longitude and latitude of the current position and other information (including applet)
- Huid learning 7: Hudi and Flink integration
猜你喜欢

快速搭建一个网关服务,动态路由、鉴权的流程,看完秒会(含流程图)

HCIP第九天_BGP实验

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

Marketing play is changeable, and understanding the rules is the key!

Why is the text box of Google material design not used?

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

Huid learning 7: Hudi and Flink integration

DIY system home page, your personalized needs PRO system to meet!
![第2章-2 计算分段函数[1]](/img/40/cad6bf92849624199af0fd1ba1d433.jpg)
第2章-2 计算分段函数[1]
![[activity registration] User Group Xi'an - empowering enterprise growth with modern data architecture](/img/92/88be42faf0451cb19067672dab69c8.jpg)
[activity registration] User Group Xi'an - empowering enterprise growth with modern data architecture
随机推荐
Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]
Go channel
Distributed system architecture theory and components
Gb/t 41479-2022 information security technology network data processing security requirements map overview
Hyperlink label
Why setting application.targetframerate doesn't work
看得清比走得快更重要,因为走得对才能走得远
When I use MySQL CDC, there are 100 million pieces of data in the source table. In the full volume phase, when I synchronize 10 million, I stop, and then pass
HCIP第九天_BGP实验
Use of tkmapper - super detailed
Competition: diabetes genetic risk detection challenge (iFLYTEK)
MDM data quality application description
Go synergy
快速搭建一个网关服务,动态路由、鉴权的流程,看完秒会(含流程图)
[opencv] generate transparent PNG image
Explain cache consistency and memory barrier
推荐一个摆脱变量名纠结的神器和批量修改文件名方法
DIY system home page, your personalized needs PRO system to meet!
Wechat applet - wechat applet browsing PDF files
Smartbi of smart smart smart software completed the c-round financing and accelerated the domestic Bi into the intelligent era