MySQL 中的 RLIKE运算符
喜欢:
MySQL 中的此运算符用于执行字符串表达式与模式的模式匹配。
句法 :
RLIKE pattern
参数 :
此方法接受语法中提到的一个参数。
- 模式 –我们要与表达式匹配的模式。下面描述了各种模式及其用法。
Pattern | What the Pattern matches |
---|---|
* | Zero or more instances of string preceding it |
+ | One or more instances of strings preceding it |
. | Any single character |
? | Match zero or one instances of the strings preceding it. |
^ | caret(^) matches Beginning of string |
$ | End of string |
[abc] | Any character listed between the square brackets |
[^abc] | Any character not listed between the square brackets |
[A-Z] | match any upper case letter. |
[a-z] | match any lower case letter |
[0-9] | match any digit from 0 through to 9. |
[[:<:]] | matches the beginning of words. |
[[:>:]] | matches the end of words. |
[:class:] | matches a character class i.e. [:alpha:] to match letters, [:space:] to match white space, [:punct:] is match punctuations and [:upper:] for upper class letters. |
p1|p2|p3 | Alternation; matches any of the patterns p1, p2, or p3 |
{n} | n instances of preceding element |
{m,n} | m through n instances of preceding element |
示例-1:
以下示例查找姓氏以字母 S 开头的员工。
CREATE TABLE Employee
(
Employee_id INT AUTO_INCREMENT,
First_name VARCHAR(100) NOT NULL,
Last_name VARCHAR(100) NOT NULL,
Joining_Date DATE NOT NULL,
PRIMARY KEY(Employee_id )
);
向 Employee 表插入一些数据:
INSERT INTO Employee
(First_name ,Last_name , Joining_Date )
VALUES
('Sayantan', 'Majumdar', '2000-01-11'),
('Anushka', 'Samanta', '2002-11-10' ),
('Sayan', 'Sharma', '2005-06-11' ),
('Shayari', 'Das', '2008-01-21' ),
('Sayani', 'Jain', '2008-02-01' ),
('Tapan', 'Samanta', '2010-01-11' ),
('Deepak', 'Sharma', '2014-12-01' ),
('Ankana', 'Jana', '2018-08-17'),
('Shreya', 'Ghosh', '2020-09-10') ;
因此,员工表如下。
select * from Employee ;
输出 :
Employee_id | First_name | Last_name | Joining_Date |
---|---|---|---|
1 | Sayantan | Majumdar | 2000-01-11 |
2 | Anushka | Samanta | 2002-11-10 |
3 | Sayan | Sharma | 2005-06-11 |
4 | Shayari | Das | 2008-01-21 |
5 | Sayani | Jain | 2008-02-01 |
6 | Tapan | Samanta | 2010-01-11 |
7 | Deepak | Sharma | 2014-12-01 |
8 | Ankana | Jana | 2018-08-17 |
9 | Shreya | Ghosh | 2020-09-10 |
现在,我们将检查姓氏为“S”的员工。
SELECT * FROM Employee
WHERE Last_name RLIKE '^S' ;
输出 :
Employee_id | First_name | Last_name | Joining_Date |
---|---|---|---|
2 | Anushka | Samanta | 2002-11-10 |
3 | Sayan | Sharma | 2005-06-11 |
6 | Tapan | Samanta | 2010-01-11 |
7 | Deepak | Sharma | 2014-12-01 |
示例 2 :
以下示例查找名字以字母“i”结尾的员工。
SELECT * FROM Employee
WHERE First_name RLIKE 'i$' ;
输出 :
Employee_id | First_name | Last_name | Joining_Date |
---|---|---|---|
4 | Shayari | Das | 2008-01-21 |
5 | Sayani | Jain | 2008-02-01 |