SQL 基础
SQL 简介
SQL 入门
SQL 语法
SQL 创建数据库
SQL 创建表
SQL Constraints
SQL Insert
SQL Select
SQL Where
SQL AND & OR
SQL IN & Between
SQL Order By
SQL Top/Limit
SQL Distinct
SQL Update
SQL Delete
SQL Truncate Table
SQL Drop
SQL 连接
SQL Joining Tables
SQL Inner Join
SQL Left Join
SQL Right Join
SQL Full Join
SQL Cross Join
高级 SQL
SQL Union
SQL Like
SQL Alter Table
SQL Aliases
SQL Group By
SQL Having
SQL Create View
SQL Create Index
SQL Dates and Times
SQL Cloning Tables
SQL 临时表
SQL 子查询
SQL 注入
SQL 参考
SQL 数据类型
MySQL 数据类型
SQL Server 数据类型
SQL 方法
SQL Like - SQL基础教程 - 笔下光年
网站首页
SQL Like
In this tutorial you will learn how to retrieve the data based on a partial match. ## Pattern Matching So far, you've seen the conditions that identify an exact string, e.g. `WHERE name='Lois Lane'`. But in SQL you can perform partial or pattern matching too using the `LIKE` operator. The `LIKE` operator provides a measure of pattern matching by allowing you to specify wildcards for one or more characters. You can use the following two wildcard characters: - The percent sign (`%`) — Matches any number of characters, even zero characters. - The underscore (`_`) — Matches exactly one character Here're some examples that show how to use the `LIKE` operator with wildcards. | Statement | Meaning | Values Returned | |------------------------|---------|------------------| | WHERE name LIKE 'Da%' | Find names beginning with Da | David, Davidson | | WHERE name LIKE '%th' | Find names ending with th | Elizabeth, Smith | | WHERE name LIKE '%on%' | Find names containing the on | Davidson, Toni | | WHERE name LIKE 'Sa_' | Find names beginning with Sa and is followed by at most one character | Sam | | WHERE name LIKE '_oy' | Find names ending with oy and is preceded by at most one character | Joy, Roy | | WHERE name LIKE '_an_' | Find names containing an and begins and ends with at most one character | Dana, Hans | | WHERE name LIKE '%ar_' | Find names containing ar, begins with any number of characters, and ends with at most one character | Richard, Karl | | WHERE name LIKE '_ar%' | Find names containing ar, begins with at most one character, and ends with any number of characters | Karl, Mariya | Let's put the statements we've discussed above into real use by searching some records. Consider we've an employees table in our database with the following records: ```sql +--------+------------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+------------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 4 | | 2 | Tony Montana | 2002-07-15 | 6500 | 1 | | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 3 | | 5 | Martin Blank | 2008-06-24 | 5600 | NULL | | 6 | simons bistro | 2009-04-01 | 6000 | 1 | +--------+------------------+------------+--------+---------+ ``` Now, let's say you want to find out all the employees whose name begins with S letter. ```sql SELECT * FROM employees WHERE emp_name LIKE 'S%'; ``` After executing the query, you'll get the output something like this: ```sql +--------+------------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+------------------+------------+--------+---------+ | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | | 6 | simons bistro | 2009-04-01 | 6000 | 1 | +--------+------------------+------------+--------+---------+ ``` In MySQL nonbinary string (`CHAR`, `VARCHAR`, `TEXT`) comparisons are case-insensitive by default, whereas binary strings (`BINARY`, `VARBINARY`, `BLOB`) comparisons are case-sensitive. This means that if you search with `WHERE name LIKE 'S%'`, you get all column values that start with S or s (as you can see we've got both "Sarah" and "simons"). However, if you want to make this search case sensitive you can use the `BINARY` operator as follow: ```sql -- Syntax for MySQL Database SELECT * FROM employees WHERE BINARY emp_name LIKE 'S%'; ``` Now, this statement will return only those employees whose name starts with capital S letter: ```sql +--------+------------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+------------------+------------+--------+---------+ | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | +--------+------------------+------------+--------+---------+ ``` <div class="callout callout-info mb-3">Note: If you want a column always to be treated in case-sensitive fashion, declare it with a case sensitive or binary collation to avoid any performance issue.</div> <div class="callout callout-success mb-3">Tip: Partial matches are useful when you don't know the exact form of the string for which you're searching. You can also use partial matching to retrieve multiple rows that contain similar strings in one of the table's columns.</div>
上一篇:
SQL Union
下一篇:
SQL Alter Table