# 查询中的运算符

## 1、算术运算符

```
+、-、*、/、%

基本算术运算：通常不在条件中使用，而是用于结果运算（select 字段中）
```

## 2、比较运算符

```
>、>=、<、<=、=、<>
通常是用来在条件中进行限定结果
=：在mysql中，没有对应的 ==比较符号，就是使用=来进行相等判断
<=>：相等比较
```

## 3、逻辑运算符

```
and、or、not
and：逻辑与
or：逻辑或
not：逻辑非
```

## 4、In运算符

In：在什么里面，是用来替代=，当结果不是一个值，而是一个结果集的时候

基本语法： in (结果1,结果2,结果3…)，只要当前条件在结果集中出现过，那么就成立

```
mysql> select * from class;
+----------+------------+--------------+
| class_id | class_name | class_number |
+----------+------------+--------------+
|        1 | 一年级     |           60 |
|        2 | 二年级     |           40 |
|        3 | 三年级     |           50 |
|        4 | 四年级     |           45 |
|        5 | 五年级     |           49 |
|        6 | 六年级     |           56 |
+----------+------------+--------------+
6 rows in set (0.04 sec)

mysql> select * from class where class_id in ('1','3','5');
+----------+------------+--------------+
| class_id | class_name | class_number |
+----------+------------+--------------+
|        1 | 一年级     |           60 |
|        3 | 三年级     |           50 |
|        5 | 五年级     |           49 |
+----------+------------+--------------+
3 rows in set (0.06 sec)
```

## 5、Is运算符

Is是专门用来判断字段是否为NULL的运算符

基本语法：is null / is not null

```
mysql> select * from class where class_number is null;
Empty set (0.09 sec)

mysql> select * from class where class_number is not null;
+----------+------------+--------------+
| class_id | class_name | class_number |
+----------+------------+--------------+
|        1 | 一年级     |           60 |
|        2 | 二年级     |           40 |
|        3 | 三年级     |           50 |
|        4 | 四年级     |           45 |
|        5 | 五年级     |           49 |
|        6 | 六年级     |           56 |
+----------+------------+--------------+
6 rows in set (0.06 sec)
```

## 6、Like运算符

Like运算符：是用来进行模糊匹配（匹配字符串）

基本语法：like ‘匹配模式’;

匹配模式中，有两种占位符：

\_：匹配对应的单个字符

%：匹配多个字符

```
mysql> select * from class where class_name like '二%';
+----------+------------+--------------+
| class_id | class_name | class_number |
+----------+------------+--------------+
|        2 | 二年级     |           40 |
+----------+------------+--------------+
1 row in set (0.01 sec)

mysql> select * from class where class_name like '二__';
+----------+------------+--------------+
| class_id | class_name | class_number |
+----------+------------+--------------+
|        2 | 二年级     |           40 |
+----------+------------+--------------+
1 row in set (0.03 sec)

mysql> select * from class where class_name like '二_';
Empty set (0.07 sec)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://beret81.gitbook.io/study-notes/mysql-shu-ju-ku/cha-xun-zhong-de-yun-suan-fu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
