操作符 作用
and 与,需要同时满足where子句中的条件
or 或,只需要匹配多个where子句中的一个条件
in 用于指定where子句查询的范围
not 非,一般与in、between and、exists一起使用,表示取反

操作符使用

数据准备

  1. SET NAMES utf8mb4;
  2. SET FOREIGN_KEY_CHECKS = 0;
  3. -- ----------------------------
  4. -- Table structure for user
  5. -- ----------------------------
  6. DROP TABLE IF EXISTS `user`;
  7. CREATE TABLE `user` (
  8. `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  9. `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名',
  10. `age` int(11) NOT NULL COMMENT '年龄',
  11. `sex` smallint(6) NOT NULL COMMENT '性别',
  12. PRIMARY KEY (`id`) USING BTREE
  13. ) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
  14. -- ----------------------------
  15. -- Records of user
  16. -- ----------------------------
  17. INSERT INTO `user` VALUES (1, '李子捌', 18, 1);
  18. INSERT INTO `user` VALUES (2, '张三', 22, 1);
  19. INSERT INTO `user` VALUES (3, '李四', 38, 1);
  20. INSERT INTO `user` VALUES (4, '王五', 25, 1);
  21. INSERT INTO `user` VALUES (5, '六麻子', 13, 0);
  22. INSERT INTO `user` VALUES (6, '田七', 37, 1);
  23. INSERT INTO `user` VALUES (7, '谢礼', 18, 0);
  24. SET FOREIGN_KEY_CHECKS = 1;
mysql> select * from user;
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  1 | 李子捌 |  18 |   1 |
|  2 | 张三   |  22 |   1 |
|  3 | 李四   |  38 |   1 |
|  4 | 王五   |  25 |   1 |
|  5 | 六麻子 |  13 |   0 |
|  6 | 田七   |  37 |   1 |
|  7 | 谢礼   |  18 |   0 |
+----+--------+-----+-----+

and操作符

查询年龄=18 并且 性别为男的用户(注意:sex=1代表男性)?

mysql> select * from user where age = 18 and sex =1;
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  1 | 李子捌 |  18 |   1 |
+----+--------+-----+-----+

or操作符

or只需要满足多个where条件中的一个即可,条件之间是’或’。

查询年龄=18 或者 性别为男的用户(注意:sex=1代表男性)?

mysql> select * from user where age = 18 or sex =1;
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  1 | 李子捌 |  18 |   1 |
|  2 | 张三   |  22 |   1 |
|  3 | 李四   |  38 |   1 |
|  4 | 王五   |  25 |   1 |
|  6 | 田七   |  37 |   1 |
|  7 | 谢礼   |  18 |   0 |
+----+--------+-----+-----+

in操作符

in操作符用于指定where子句的查询范围。’包含’的意思,可以用多个or操作符来实现。

查询name等于张三、李四、王五的用户信息?

mysql> select * from user where name in ('张三', '李四', '王五');
+----+------+-----+-----+
| id | name | age | sex |
+----+------+-----+-----+
|  2 | 张三 |  22 |   1 |
|  3 | 李四 |  38 |   1 |
|  4 | 王五 |  25 |   1 |
+----+------+-----+-----+

not操作符

查询某个值不在什么范围之内、不存在,可以使用not操作符,not操作符不单独使用,它经常和in操作符、like操作符、between and、exists等一起使用。

not in:查询姓名不等于张三、李四、王五的用户信息?

mysql> select * from user where name not in ('张三', '李四', '王五');
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  1 | 李子捌 |  18 |   1 |
|  5 | 六麻子 |  13 |   0 |
|  6 | 田七   |  37 |   1 |
|  7 | 谢礼   |  18 |   0 |
+----+--------+-----+-----+

not like:查询姓名不是以李子开头的用户?

mysql> select * from user where name not like '李子%';
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  2 | 张三   |  22 |   1 |
|  3 | 李四   |  38 |   1 |
|  4 | 王五   |  25 |   1 |
|  5 | 六麻子 |  13 |   0 |
|  6 | 田七   |  37 |   1 |
|  7 | 谢礼   |  18 |   0 |
+----+--------+-----+-----+

not between and:查询年龄不属于20 - 30之间的用户?

mysql> select * from user where age not between 20 and 30;
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  1 | 李子捌 |  18 |   1 |
|  3 | 李四   |  38 |   1 |
|  5 | 六麻子 |  13 |   0 |
|  6 | 田七   |  37 |   1 |
|  7 | 谢礼   |  18 |   0 |
+----+--------+-----+-----+

not exists
与exists用法一致,用于判断当前where子句的结果是否应该返回。not exists 和 exists作用于一个子查询,向上级返回true和false;
示例语法:
SELECT … FROM table WHERE EXISTS (sub query)
SELECT … FROM table WHERE NOT EXISTS (sub query)

数据准备

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for order
-- ----------------------------
DROP TABLE IF EXISTS `order`;
CREATE TABLE `order`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '订单号',
  `user_id` bigint(20) NULL DEFAULT NULL COMMENT '用户id',
  `price` decimal(10, 2) NULL DEFAULT NULL COMMENT '金额',
  `create_date` datetime(0) NULL DEFAULT NULL COMMENT '创建日期',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of order
-- ----------------------------
INSERT INTO `order` VALUES (1, 'DD-20211110-000001', 1, 250.00, '2021-11-10 22:37:19');

SET FOREIGN_KEY_CHECKS = 1;
mysql> select * from `order`;
+----+--------------------+---------+--------+---------------------+
| id | number             | user_id | price  | create_date         |
+----+--------------------+---------+--------+---------------------+
|  1 | DD-20211110-000001 |       1 | 250.00 | 2021-11-10 22:37:19 |
+----+--------------------+---------+--------+---------------------+

exists:查询已下单的用户信息?

mysql> select * from user where exists(select id from `order` where user_id = user.id);
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  1 | 李子捌 |  18 |   1 |
+----+--------+-----+-----+

not exists:查询已未单的用户信息?

mysql> select * from user where not exists (select id from `order` where user_id = user.id);
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  2 | 张三   |  22 |   1 |
|  3 | 李四   |  38 |   1 |
|  4 | 王五   |  25 |   1 |
|  5 | 六麻子 |  13 |   0 |
|  6 | 田七   |  37 |   1 |
|  7 | 谢礼   |  18 |   0 |
+----+--------+-----+-----+

操作符顺序

这是因为and的优先级比or高

查询用户表中,年龄大于20岁或者性别为男,并且姓名不等于张三的用户?

mysql> select * from user where age > 20 or sex = 1 and name != '张三';
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  1 | 李子捌 |  18 |   1 |
|  2 | 张三   |  22 |   1 |
|  3 | 李四   |  38 |   1 |
|  4 | 王五   |  25 |   1 |
|  6 | 田七   |  37 |   1 |
+----+--------+-----+-----+

注:SQL解析器把上面的SQL解析成sex = 1 and name != ‘张三’ or age > 20 ;出现了张三,需使用括号解决。

mysql> select * from user where (age > 20 or sex = 1) and name != '张三';
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  1 | 李子捌 |  18 |   1 |
|  3 | 李四   |  38 |   1 |
|  4 | 王五   |  25 |   1 |
|  6 | 田七   |  37 |   1 |
+----+--------+-----+-----+

distinct

查询列值不重复的数据,使用distinct关键字去重。

获取user表中的用户有哪些年龄。使用distinct关键字,应用于需要去重的列前面

mysql> select distinct age from user;

注:distinct关键字去重会作用于所有的字段,如果distinct关键字后面跟了多个字段,那么多个字段的值都不相等才算不重复。

mysql> select distinct age,name from user;
+-----+--------+
| age | name   |
+-----+--------+
|  18 | 李子捌 |
|  22 | 张三   |
|  38 | 李四   |
|  25 | 王五   |
|  13 | 六麻子 |
|  37 | 田七   |
|  18 | 谢礼   |
+-----+--------+

出现两个18,因为没有满足多个字段都不同条件。

limit

使用limit关键字限制返回的行。limit的取值需大于等于0的整数 ,如果传入负数和小数会报错。

mysql> select * from user limit 0;
Empty set (0.00 sec)

mysql> select * from user limit 1;
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  1 | 李子捌 |  18 |   1 |
+----+--------+-----+-----+

limit可以跟两个参数分别表示起始值和结束值,闭区间(包含起始值和结束值)。如果跟一个参数,则表示结束值,起始值默认为0。
limit 2, 4表示查询第三条数据到第五条数据,其行号为2到4。

mysql> select * from user limit 2, 4;
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  3 | 李四   |  38 |   1 |
|  4 | 王五   |  25 |   1 |
|  5 | 六麻子 |  13 |   0 |
|  6 | 田七   |  37 |   1 |
+----+--------+-----+-----+