MariaDB Like子句

在MariaDB中,當操作需要完全匹配時,LIKE子句與SELECT語句一起使用來檢索數據。它可以與SELECTINSERTUPDATEDELETE語句一起使用。

它用於模式匹配並返回truefalse。用於比較的模式接受以下通配符:

  • "%"通配符:匹配字符數(0或更多)。
  • "_"通配符:匹配單個字符。它匹配其集合中的字符。

語法:

SELECT field, field2,... FROM table_name, table_name2,...  
WHERE field LIKE condition

1. 使用%通配符(百分號通配符)

假設我們有一個students表,並有以下數據。

MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |
|          3 | JMaster      | Beijing         | 2016-05-07     |
|          4 | Mahesh       | Guangzhou       | 2016-06-07     |
|          5 | Kobe         | Shanghai        | 2016-02-07     |
|          6 | Blaba        | Shengzhen       | 2016-08-07     |
|          7 | Maxsu        | Sanya           | 2017-08-08     |
+------------+--------------+-----------------+----------------+
6 rows in set (0.00 sec)

現在想要查詢那些名字以Ma字母開頭的所有學生信息,那麼就可以使用LIKE條件的通配符來查找所有以Ma開頭的名字。參考以下查詢語句 -

SELECT student_name  
FROM students  
WHERE student_name LIKE 'Ma%';

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> SELECT student_name
    -> FROM students
    -> WHERE student_name LIKE 'Ma%';
+--------------+
| student_name |
+--------------+
| Maxsu        |
| Mahesh       |
| Maxsu        |
+--------------+
3 rows in set (0.07 sec)

也可以在同一個字符串中多次使用通配符。例如,要查詢名字中包含'Ma'字符的所有記錄 -

SELECT student_name  
FROM students
WHERE student_name LIKE '%Ma%';

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> SELECT student_name
    -> FROM students
    -> WHERE student_name LIKE '%Ma%';
+--------------+
| student_name |
+--------------+
| Maxsu        |
| JMaster      |
| Mahesh       |
| Maxsu        |
+--------------+
4 rows in set (0.00 sec)

2. 使用_通配符(下劃線通配符)

使用帶LIKE條件的通配符。`(下劃線)通配符只檢查一個字符。下面語句將查詢名字爲「Max_u」`的學生信息。

SELECT *  
FROM students
WHERE student_name LIKE 'Max_u';

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> SELECT *
    -> FROM students
    -> WHERE student_name LIKE 'Max_u';
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |
|          7 | Maxsu        | Sanya           | 2017-08-08     |
+------------+--------------+-----------------+----------------+
2 rows in set (0.00 sec)

3. LIKE子句使用NOT運算符

在MariaDB中,LIKE子句可以使用NOT運算符。在NOT運算符中使用通配符。 在這個示例中,將是查詢名字不是以"Ma"開頭的所有學生信息。

SELECT *  
FROM students
WHERE student_name NOT LIKE 'Ma%';

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> SELECT *
    -> FROM students
    -> WHERE student_name NOT LIKE 'Ma%';
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          3 | JMaster      | Beijing         | 2016-05-07     |
|          5 | Kobe         | Shanghai        | 2016-02-07     |
|          6 | Blaba        | Shengzhen       | 2016-08-07     |
+------------+--------------+-----------------+----------------+
3 rows in set (0.00 sec)