mysql伪列能做查询条件吗_使用mysql like条件来做查询

Summary: in this tutorial, you will learn how to use MySQL LIKEoperator to select data based on patterns.

The LIKEoperator is commonly used to select data based on patterns. Using the LIKEoperatorin appropriate way is essential to increase the query performance.

The LIKEoperator allows you to select data from a table based on a specified pattern. Therefore the LIKEoperator is often used in the WHERE clause of the SELECT statement.

MySQL provides two wildcard characters for using with the LIKEoperator, the percentage % and underscore _.The percentage ( %) wildcard allows you to match any string of zero or more characters.

The underscore ( _) wildcard allows you to match any single character.

MySQL LIKE examples

Let’s practice with some examples of how to use the LIKEoperator.

MySQL LIKE with percentage (%) wildcard

Suppose you want to search for employee whose first name starts with character ‘ a‘, you can use the percentage wildcard ( %) at the end of the pattern as follows:SELECT employeeNumber, lastName, firstName FROM employees WHERE firstName LIKE 'a%'

AAffA0nNPuCLAAAAAElFTkSuQmCC

MySQL scans the whole employees table to find employee whose first name starts with character ‘ a’ and followed by any number of characters.

To search for employee whose last name ends with ‘ on‘ string e.g., Patterson, Thompson, you can use the %wildcard at the beginning of the pattern as the following query:SELECT employeeNumber, lastName, firstName FROM employees WHERE lastName LIKE '%on'

AAffA0nNPuCLAAAAAElFTkSuQmCC

If you know the searched string is embedded inside in the column, you can use the percentage ( %) wildcard at the beginning and the end of the pattern. For example, to find all employees whose last names contain ‘ on‘ string, you can execute following query:SELECT employeeNumber, lastName, firstName FROM employees WHERE lastname LIKE '%on%'

AAffA0nNPuCLAAAAAElFTkSuQmCC

MySQL LIKE with underscore( _) wildcard

To find employee whose first name starts with T, ends with mand contains any single character between e.g., Tom, Tim, you use the underscore wildcard to construct the pattern as follows:SELECT employeeNumber, lastName, firstName FROM employees WHERE firstname LIKE 'T_m'

AAffA0nNPuCLAAAAAElFTkSuQmCC

MySQL LIKE operator with NOT operator

The MySQL allows you to combine the NOToperator with the LIKEoperator to find string that does not match a specific pattern.

Suppose you want to search for employee whose last name does not start with character ‘ B‘, you can use the NOT LIKE with the pattern as the following query:SELECT employeeNumber, lastName, firstName FROM employees WHERE lastName NOT LIKE 'B%'

AAffA0nNPuCLAAAAAElFTkSuQmCC

Notice that the pattern is not case sensitive with the LIKEoperator therefore the ‘b%’ and ‘B%’ patterns produce the same result.

MySQL LIKE with ESCAPE clause

Sometimes the pattern, which you want to match, contains wildcard character e.g., 10%, _20… etc. In this case, you can use the ESCAPE clause to specify the escape character so that MySQL interprets the wildcard character as literal character. If you don’t specify the escape character explicitly, the backslash character ‘ \‘ is the default escape character.

For example, if you want to find product whose product code contains string _20, you can perform following query:SELECT productCode, productName FROM products WHERE productCode LIKE '%\_20%'

Or specify a different escape character e.g., ‘ $‘ by using the ESCAPE clause:SELECT productCode, productName FROM products WHERE productCode LIKE '%$_20%' ESCAPE '$'

AAffA0nNPuCLAAAAAElFTkSuQmCC

The pattern %$_20% matches any string that contains _20 string.

The LIKEoperator forces MySQL to scan the whole table to find the matching rows therefore it does not allow the database engine to useindex for fast searching. As the result, the performance of the query that uses the LIKEoperator degrades when you query data from a table with a large number of rows.

In this tutorial, you have learned how to use the LIKEoperator to query data based on patterns, which is more flexible than using comparison operators.

Related Tutorials

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值