学堂 学堂 学堂公众号手机端

在MariaDB中LIKE子句有哪些用法,用于做什么

lewis 5年前 (2020-02-07) 阅读数 4 #技术
关于“在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)

以上就是关于“在MariaDB中LIKE子句有哪些用法,用于做什么”的相关知识,感谢各位的阅读,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注博信,小编每天都会为大家更新不同的知识。
版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门