MySQL (17) --- LIKE clause

MySQL LIKE clause

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

WHERE clause can use the = sign to the setting condition acquiring data, such as "runoob_author = 'RUNOOB.COM'".

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

SQL LIKE clause using percent% character to represent any character, similar to the UNIX regular expression or asterisk *.

Without using percent%, LIKE clauses and the = the effect is the same.

grammar

The following is the general syntax of SQL SELECT statement using the LIKE clause to read data from the data table:

SELECT field1, field2,...fieldN 
FROM table_name
WHERE 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 usually used in conjunction with%, similar to the search for a meta-characters.
  • You can use AND or OR specify one or more conditions.
  • You can use WHERE ... LIKE clause to specify the conditions in DELETE, or UPDATE command.

Use the command prompt LIKE clause

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

Examples

Here's what we will get all the records runoob_author field to COM at the end of the runoob_tbl table:

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         | 学习 Java   | RUNOOB.COM    | 2015-05-01      |
| 4         | 学习 Python | RUNOOB.COM    | 2016-03-06      |
+-----------+---------------+---------------+-----------------+
2 rows in set (0.01 sec)

 

Guess you like

Origin blog.csdn.net/zhangbijun1230/article/details/92382362