Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of LIKE clause in MySQL

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "the usage of LIKE clause in MySQL". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the usage of LIKE clause in MySQL.

MySQL LIKE clause

We know that the SQL SELECT command is used in MySQL to read data, and we can use the WHERE clause in the SELECT statement to get the specified record.

You can use the equal sign = in the WHERE clause to set conditions for getting data, such as "runoob_author = 'RUNOOB.COM'".

But sometimes we need to get all the records whose runoob_author field contains the character "COM", and then we need to use the SQL LIKE clause in the WHERE clause.

The percent sign% character is used in the SQL LIKE clause to represent any character, similar to the asterisk * in UNIX or regular expressions.

If the% sign is not used, the LIKE clause has the same effect as the equal sign =.

Grammar

The following is the general syntax for SQL SELECT statements to read data from a data table using the LIKE clause:

SELECT field1, field2,...fieldN FROM table_nameWHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'

You can specify any condition in the WHERE clause.

You can use the like clause in the WHERE clause.

You can use the LIKE clause instead of the equal sign =.

LIKE is usually used with%, similar to a metacharacter search.

You can specify one or more conditions using AND or OR.

You can use the WHERE...LIKE clause in the DELETE or UPDATE command to specify the condition.

Use the LIKE clause at the command prompt

Next we will use the WHERE...LIKE clause in the SQL SELECT command to read data from the MySQL data table runoob_tbl.

Example

Here are all the records that end with COM in the runoob_author field in the runoob_tbl table:

SQL UPDATE statement:

Mysql > use RUNOOB;Database changedmysql > SELECT * from runoob_tbl WHERE runoob_author LIKE'% COM' +-+ | runoob_id | runoob_title | runoob_author | submission_date | +- -+-+ | 3 | Learning Java | RUNOOB.COM | 2015-05-01 | | 4 | Learning Python | RUNOOB.COM | 2016-03-06 | +-+ 2 rows in set (0.01sec)

Using the LIKE clause in PHP scripts

You can use the mysqli_query () of the PHP function and the same SQL SELECT command with the WHERE...LIKE clause to get the data.

This function is used to execute the SQL command and then output the data of all queries through the PHP function mysqli_fetch_assoc ().

However, if it is an S QL statement in DELETE or UPDATE that uses the WHERE...LIKE clause, you do not need to use the mysqli_fetch_array () function.

Example

Here are all the records that end with COM in the runoob_author field in the runoob_tbl table using the PHP script:

MySQL DELETE clause test:

The output is shown in the following figure:

Thank you for your reading. The above is the content of "the usage of LIKE clause in MySQL". After the study of this article, I believe you have a deeper understanding of the usage of LIKE clause in MySQL, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report