当前位置:网站首页>10. Learn MySQL like clause
10. Learn MySQL like clause
2022-07-28 09:04:00 【Microservice spring cloud】
MySQL LIKE Clause
We know that MySQL Use in SQL SELECT Command to read data , At the same time, we can go to SELECT Use in statement WHERE Clause to get the specified record .
WHERE An equal sign can be used in a clause = To set the conditions for obtaining data , Such as "xxxxxx_author = 'XXXXXX.COM'".
But sometimes we need to get xxxxxx_author Field contains "COM" All records of characters , Then we need to WHERE Used in clauses SQL LIKE Clause .
SQL LIKE Use percent sign in Clause % Character to represent any character , Be similar to UNIX Or an asterisk in a regular expression *.
If you don't use the percent sign %, LIKE Clause and equal sign = The effect is the same .
grammar
Here are SQL SELECT Statements use LIKE Clause to read data from a data table :
SELECT field1, field2,...fieldN
FROM table_name
WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'- You can WHERE Clause to specify any condition .
- You can WHERE Used in clauses LIKE Clause .
- You can use LIKE Clause instead of equal sign =.
- LIKE Usually with % Use together , Similar to a metacharacter search .
- You can use AND perhaps OR Specify one or more conditions .
- You can DELETE or UPDATE Use in command WHERE...LIKE Clause to specify the condition .
Use... At the command prompt LIKE Clause
Below we will be in SQL SELECT Use in command WHERE...LIKE Clause comes from MySQL Data sheet xxxxxx_tbl Read data from .
example
Here is what we will xxxxxx_tbl Table acquisition xxxxxx_author In the field, use COM For all records at the end :
SQL LIKE sentence :
mysql> use XXXXXX;
Database changed
mysql> SELECT * from xxxxxx_tbl WHERE xxxxxx_author LIKE '%COM';
+-----------+---------------+---------------+-----------------+
| xxxxxx_id | xxxxxx_title | xxxxxx_author | submission_date |
+-----------+---------------+---------------+-----------------+
| 3 | Study Java | XXXXXX.COM | 2021-11-20 |
| 4 | Study Python | XXXXXX.COM | 2021-11-21 |
+-----------+---------------+---------------+-----------------+
2 rows in set (0.01 sec)stay PHP Use in script LIKE Clause
You can use PHP Functional mysqli_query() And the same SQL SELECT close WHERE...LIKE Clause to get data .
This function is used to execute SQL command , And then through PHP function mysqli_fetch_array() To output all the query data .
But if it is DELETE perhaps UPDATE Use in WHERE...LIKE Clause S QL sentence , No need to use mysqli_fetch_array() function .
example
Here's how we use PHP Script in xxxxxx_tbl Read... In table xxxxxx_author In the field, use COM For all records at the end :
MySQL LIKE Clause test :
<?php
$dbhost = 'localhost'; // mysql Server host address
$dbuser = 'root'; // mysql user name
$dbpass = '123456'; // mysql User name, password
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die(' The connection fails : ' . mysqli_error($conn));
}
// Set encoding , Prevent Chinese miscoding
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(' Unable to read data : ' . mysqli_error($conn));
}
echo '<h2>xx course mysqli_fetch_array test <h2>';
echo '<table border="1"><tr><td> course ID</td><td> title </td><td> author </td><td> Submission date </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 to obtain the subordinate / annotation information of KEGG channel
- 图片批处理|必备小技能
- 完善的交叉编译环境记录 peta 生成的shell 脚本
- Two dimensional array and operation
- 快速搭建一个网关服务,动态路由、鉴权的流程,看完秒会(含流程图)
- Data analysis interview question summary
- MDM数据质量应用说明
- Top all major platforms, 22 versions of interview core knowledge analysis notes, strong on the list
- JSON 文件存储
- Machine learning (11) -- time series analysis
猜你喜欢
![[soft test software evaluator] 2013 comprehensive knowledge over the years](/img/c5/183acabd7015a5e515b7d83c127b2c.jpg)
[soft test software evaluator] 2013 comprehensive knowledge over the years

分布式系统架构理论与组件

Review the past and know the new MySQL isolation level

The chess robot pinched the finger of a 7-year-old boy, and the pot of a software testing engineer? I smiled.

Ciou loss

View the dimensions of the list

Line generation (matrix)

JSON 文件存储

Flink window & time principle

Baidu AI Cloud Jiuzhou district and county brain, depicting a new blueprint for urban and rural areas!
随机推荐
Recycling of classes loaded by classloader
Service current limiting and fusing of micro service architecture Sentinel
[soft test software evaluator] 2013 comprehensive knowledge over the years
快速搭建一个网关服务,动态路由、鉴权的流程,看完秒会(含流程图)
Introduction of functions in C language (blood Book 20000 words!!!)
Hcip day 9_ BGP experiment
剑指offer
Let me teach you how to assemble a registration center?
Hcip day 8
Argocd Web UI loading is slow? A trick to teach you to solve
After reading these 12 interview questions, the new media operation post is yours
C language array pointer and pointer array discrimination, analysis of memory leakage
Ciou loss
How can MySQL query judge whether multiple field values exist at the same time
Use of tkmapper - super detailed
训练一个自己的分类 | 【包教包会,数据都准备好了】
JSON file storage
CSV文件存储
No one wants to tell the truth about kubernetes secret
Feign调用异常[Running, pool size = 10, active threads = 10, queued tasks = 0, completed tasks = n]