众所周知,LIKE运算符与WILDCARD字符一起使用来获取具有指定字符串的字符串。基本上,WILDCARD是帮助搜索与复杂条件匹配的数据的字符。以下是可与LIKE运算符结合使用的通配符类型
'%'通配符用于指定0、1或多个字符的模式。使用%通配符的基本语法如下
Select Statement…Where column_name LIKE ‘X%’
X是任何指定的起始模式,例如more和%的单个字符与从0开始的任意数量的字符匹配。
百分比“%”通配符可以采用多种方式使用指定模式。以下是一些示例,其中显示了不同的LIKE运算符(含%)。在这些示例中,X代表指定的模式。
1. 像“ X%”:-它会找到任何以“ X”开头的值。
例
mysql> Select * from Student Where name LIKE 'a%'; +------+-------+---------+---------+ | Id | Name | Address | Subject | +------+-------+---------+---------+ | 2 | Aarav | Mumbai | History | +------+-------+---------+---------+ 1 row in set (0.00 sec)
2.喜欢'%X':-它会找到任何以“ X”结尾的值。
例
mysql> Select * from Student Where name LIKE '%v'; +------+--------+---------+-----------+ | Id | Name | Address | Subject | +------+--------+---------+-----------+ | 1 | Gaurav | Delhi | Computers | | 2 | Aarav | Mumbai | History | | 20 | Gaurav | Jaipur | Computers | +------+--------+---------+-----------+ 3 rows in set (0.00 sec)
3.像'%X%':-它将发现任何位置任何位置带有“ X”的值。
例
mysql> Select * from Student Where name LIKE '%h%'; +------+---------+---------+----------+ | Id | Name | Address | Subject | +------+---------+---------+----------+ | 15 | Harshit | Delhi | Commerce | | 21 | Yashraj | NULL | Math | +------+---------+---------+----------+ 2 rows in set (0.00 sec)
4. 像'X%X':-它将找到任何以“ X”开头和以“ X”结尾的值。
例
mysql> Select * from Student Where name LIKE 'a%v'; +------+-------+---------+---------+ | Id | Name | Address | Subject | +------+-------+---------+---------+ | 2 | Aarav | Mumbai | History | +------+-------+---------+---------+ 1 row in set (0.00 sec)
下划线通配符用于精确匹配一个字符。使用_通配符的基本语法如下:-Select Statement…where column_name LIKE'X_'
X是任何指定的起始模式,例如more和_的单个字符正好匹配一个字符。
下划线“ _”通配符可以与指定模式一起以多种方式单独使用或与%组合使用。以下示例显示了不同的LIKE运算符(含%)。在这些示例中,X代表指定的模式。
1.类似于“ X _”:-它会找到任何以“ X”开头且在X之后仅一个字符的值。
例
mysql> Select * from student WHERE year_of_Admission LIKE '200_'; +------+---------+---------+-----------+--------------------+ | Id | Name | Address | Subject | year_of_Admission | +------+---------+---------+-----------+--------------------+ | 1 | Gaurav | Delhi | Computers | 2001 | | 15 | Harshit | Delhi | Commerce | 2009 | | 21 | Yashraj | NULL | Math | 2000 | +------+---------+---------+-----------+--------------------+ 3 rows in set (0.00 sec)
2. 类似于“ _X”:-它会找到所有以“ X”结尾并且在X之前仅一个字符的值。
例
mysql> Select * from student WHERE year_of_Admission LIKE '_017'; +------+--------+---------+-----------+--------------------+ | Id | Name | Address | Subject | year_of_Admission | +------+--------+---------+-----------+--------------------+ | 20 | Gaurav | Jaipur | Computers | 2017 | +------+--------+---------+-----------+--------------------+ 1 row in set (0.00 sec)
3. LIKE _X%:-它与%通配符结合使用。它将查找第二个位置具有X的任何值。
例
mysql> Select * from student WHERE Name LIKE '_a%'; +------+---------+---------+-----------+--------------------+ | Id | Name | Address | Subject | year_of_Admission | +------+---------+---------+-----------+--------------------+ | 1 | Gaurav | Delhi | Computers | 2001 | | 2 | Aarav | Mumbai | History | 2010 | | 15 | Harshit | Delhi | Commerce | 2009 | | 20 | Gaurav | Jaipur | Computers | 2017 | | 21 | Yashraj | NULL | Math | 2000 | +------+---------+---------+-----------+--------------------+ 5 rows in set (0.00 sec)
4. LIKE X _%_%:- 它与%通配符结合使用。它将找到任何以X开头且长度至少为三个字符的值。
例
mysql> Select * from student WHERE Name LIKE 'g_%_%'; +------+--------+---------+-----------+--------------------+ | Id | Name | Address | Subject | year_of_Admission | +------+--------+---------+-----------+--------------------+ | 1 | Gaurav | Delhi | Computers | 2001 | | 20 | Gaurav | Jaipur | Computers | 2017 | +------+--------+---------+-----------+--------------------+ 2 rows in set (0.00 sec)