| >, >=, <, <= | 大于,大于等于,小于,小于等于 |

| = | 等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL |

| <=> | 等于, NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1) |

| !=, <> | 不等于 |

| BETWEEN a0 AND a1 | 范围匹配, [a0, a1],如果 a0 <= value <= a1,返回 TRUE(1) |

| IN (option, …) | 如果是 option 中的任意一个,返回 TRUE(1) |

| IS NULL | 是 NULL |

| IS NOT NULL | 不是 NULL |

| LIKE | 模糊匹配。 % 表示任意多个( 包括 0 个)任意字符; _ 表示任意一个字符 |

逻辑运算符

| 算符 | 说明 |

| — | — |

| AND逻辑与 | 多个条件必须都为 TRUE(1),结果才是 TRUE(1) |

| OR 逻辑或 | 任意一个条件为 TRUE(1), 结果为 TRUE(1) |

| NOT逻辑取反 | 条件为 TRUE(1),结果为 FALSE(0) |

3.7.1 查询数据中chinese 为null 的记录.

错误的写法 null = null

mysql 表有数据 修改id自增_Java

正确写法:

select * from exam_result where chinese <=> null;

select * from exam_result where chinese is null;

mysql 表有数据 修改id自增_mysql 表有数据 修改id自增_02

3.7.2 查找英语成绩不及格的同学信息

select * from exam_result where english < 60;

mysql 表有数据 修改id自增_java_03

3.7.3 查找语文比英语好的同学信息

select * from exam_result where chinese > english ;

mysql 表有数据 修改id自增_Java_04

3.7.4 查找总分在200分以下的同学

where 中不能使用别名

mysql 表有数据 修改id自增_mysql 表有数据 修改id自增_05

select name,chinese + math + english as total from exam_result where chinese + math + english > 200;

mysql 表有数据 修改id自增_mysql_06

3.7.5 查找语文成绩大于80并且英语成绩也大于80的同学

select * from exam_result where chinese > 80 and english > 80;

mysql 表有数据 修改id自增_Java_07

3.7.6 查找语文成绩大于80或者英语大于80的同学

select * from exam_result where chinese > 80 or english > 80;

mysql 表有数据 修改id自增_Java_08

注: AND的优先级高于OR,在同时使用时,需要使用小括号()包裹优先执行的部分

3.7.7 查找语文成绩在[80,90]之间的同学

select * from exam_result where chinese between 80 and 90;

mysql 表有数据 修改id自增_mysql_09

select * from exam_result where chinese >= 80 and chinese <= 90;

mysql 表有数据 修改id自增_mysql 表有数据 修改id自增_10

3.7.8 查找数学成绩是 45.0 或者 60.0 的

select * from exam_result where math in (45.0,60.0);

mysql 表有数据 修改id自增_mysql 表有数据 修改id自增_11

select * from exam_result where math = 45.0 or math = 60.0;

mysql 表有数据 修改id自增_mysql 表有数据 修改id自增_12

3.7.9 查找所有姓孙的同学的成绩

select * from exam_result where name like ‘孙%’;

mysql 表有数据 修改id自增_Java_13

select * from exam_result where name like ‘孙_’;

mysql 表有数据 修改id自增_数据库_14

3.7.10 查找所有同学中语文成绩 9 开头的.

select * from exam_result where chinese like ‘9%’;

mysql 表有数据 修改id自增_mysql_15

3.8 分页查找


– 从 0 开始,筛选 n 条结果

SELECT … FROM table_name [WHERE …] [ORDER BY …] LIMIT n;

– 从 s 开始,筛选 n 条结果

SELECT … FROM table_name [WHERE …] [ORDER BY …] LIMIT s, n;

– 从 s 开始,筛选 n 条结果,比第二种用法更明确,建议使用

SELECT … FROM table_name [WHERE …] [ORDER BY …] LIMIT n OFFSET s;

3.8.1 查找同学信息中总分最高的前3名.

select name,chinese + math + english as total from exam_result order by total desc limit 3;

mysql 表有数据 修改id自增_数据库_16

3.8.2 查找同学信息中总分最高的 4 ~ 6

select name,chinese + math + english as total from exam_result order by total desc limit 3 offset 3;

mysql 表有数据 修改id自增_mysql 表有数据 修改id自增_17

3.8.3 如果limit 后面的数字太大,超出了记录的数目,返回的结果不会有任何的错误

select name,chinese + math + english as total from exam_result order by total desc limit 10 offset 3;

mysql 表有数据 修改id自增_数据库_18

3.8.4 如果 offset 过大,得到的结果可能是一个空的

select name,chinese + math + english as total from exam_result order by total desc limit 3 offset 100;

mysql 表有数据 修改id自增_Java_19

4. 修改( Update)

==================================================================================

update [表名] set [列名] = [修改的值], [列名] = [修改的值] where 子句;

4.1 把孙悟空的数学成绩改成80


update exam_result set math = 80 where name = ‘孙悟空’;

mysql 表有数据 修改id自增_java_20

4.2 把曹孟德数学成绩改成50分,语文成绩改成95分.


update exam_result set math = 50,chinese = 95 where name = ‘曹孟德’;

mysql 表有数据 修改id自增_mysql 表有数据 修改id自增_21

4.3 所有同学的语文成绩都 -10分.


mysql> update exam_result set chinese = chinese - 10 ;