Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL LIKE Clause

MySQL LIKE Clause

We know that in MySQL we use the SELECT command to read data, and we can use a WHERE clause in the SELECT statement to obtain specified records.

In a WHERE clause, you can use the equals sign = to set the condition for obtaining data, such as "runoob_author = 'RUNOOB.COM'".

But sometimes we need to obtain all records where the runoob_author field contains the characters "COM". In this case, we need to use the LIKE clause in the WHERE clause.

LIKE

The clause is a keyword used in MySQL for fuzzy matching in the WHERE clause. It is usually used together with wildcards to search for strings that match a certain pattern.

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

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

Syntax

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



SELECT column1, column2, ...

FROM table_name

WHERE column_name LIKE pattern;

Parameter Description:

More notes:

Example

The following are some examples of using the LIKE clause.

  1. The percent wildcard %:

The % wildcard represents zero or more characters. For example, 'a%' matches any string beginning with the letter 'a' a.

SELECT * FROM customers WHERE last_name LIKE 'S%';

The above SQL statement will select all customers whose last name begins with 'S'.

  1. The underscore wildcard _:

The _ wildcard represents one character. For example, '_r%' matches strings whose second letter is 'r'. 'r' .



SELECT * FROM products WHERE product_name LIKE '_a%';

The above SQL statement will select all products whose product name has 'a' as its second character.

  1. Combining % and _:


SELECT * FROM users WHERE username LIKE 'a%o_';

The above SQL statement will match strings that begin with the letter 'a', followed by zero or more characters, then 'o', and finally any character, such as 'aaron' and 'apol'.

  1. Case-insensitive matching:


SELECT * FROM employees WHERE last_name LIKE 'smi%' COLLATE utf8mb4_general_ci;

The above SQL statement will select all employees whose last name begins with a 'smi' specified letter, regardless of case.

The LIKE clause provides powerful fuzzy search capabilities and can be customized according to different patterns and needs. When using it, make sure you understand the meaning of the wildcards and match according to the actual situation.


Using the LIKE Clause in the Command Prompt

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

Example

The following retrieves all records from the runoob_tbl table where the runoob_author field ends with COM:

SQL LIKE Statement:

mysql

use

RUNOOB

;

Database

changed

mysql

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.01

sec

)


Using the LIKE Clause in a PHP Script

You can use PHP's mysqli_query() function and the same SELECT command with a WHERE...LIKE clause to retrieve data.

This function is used to execute SQL commands, and then the PHP function mysqli_fetch_array() is used to output all the queried data.

However, if it is a DELETE or UPDATE SQL statement using a WHERE...LIKE clause, there is no need to use the mysqli_fetch_array() function.

Example

The following uses a PHP script to read all records from the runoob_tbl table where the runoob_author field ends with COM:

MySQL LIKE Clause Test:

Runoob Tutorial mysqli_fetch_array Test

' ; echo ' ' ; while ( $row = mysqli_fetch_array ( $retval , MYSQLI_ASSOC ) ) { echo " " . " " . " " . " " . " " ; } echo '
Tutorial ID Title Author Submission Date
{$row['runoob_id']}{$row['runoob_title']}{$row['runoob_author']}{$row['submission_date']}
' ; mysqli_close ( $conn ) ; ?>

The output is shown in the figure below: