文章目录
student.sql建表
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for class
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`class_num` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '班级号',
`class_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '班级名称',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact;
-- ----------------------------
-- Records of class
-- ----------------------------
INSERT INTO `class` VALUES (1, '20201001', '软件工程');
INSERT INTO `class` VALUES (2, '20201002', '计算机科学');
INSERT INTO `class` VALUES (3, '20201003', '网络工程');
INSERT INTO `class` VALUES (4, '20201005', '数学');
-- ----------------------------
-- Table structure for relationship
-- ----------------------------
DROP TABLE IF EXISTS `relationship`;
CREATE TABLE `relationship` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sno` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '学号',
`cno` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '课程号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 16 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact;
-- ----------------------------
-- Records of relationship
-- ----------------------------
INSERT INTO `relationship` VALUES (1, '202001', '1001');
INSERT INTO `relationship` VALUES (2, '202002', '1001');
INSERT INTO `relationship` VALUES (3, '202003', '1001');
INSERT INTO `relationship` VALUES (4, '202004', '1001');
INSERT INTO `relationship` VALUES (5, '202005', '1001');
INSERT INTO `relationship` VALUES (6, '202006', '1002');
INSERT INTO `relationship` VALUES (7, '202007', '1002');
INSERT INTO `relationship` VALUES (8, '202008', '1002');
INSERT INTO `relationship` VALUES (9, '202009', '1002');
INSERT INTO `relationship` VALUES (10, '202010', '1002');
INSERT INTO `relationship` VALUES (11, '202011', '1003');
INSERT INTO `relationship` VALUES (12, '202012', '1003');
INSERT INTO `relationship` VALUES (13, '202013', '1003');
INSERT INTO `relationship` VALUES (14, '202014', '1003');
INSERT INTO `relationship` VALUES (15, '202015', '1003');
-- ----------------------------
-- Table structure for scoure
-- ----------------------------
DROP TABLE IF EXISTS `scoure`;
CREATE TABLE `scoure` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '选课表',
`cno` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '课程号',
`gradeName` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '课程名称',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact;
-- ----------------------------
-- Records of scoure
-- ----------------------------
INSERT INTO `scoure` VALUES (1, '1001', '数学');
INSERT INTO `scoure` VALUES (2, '1002', '语文');
INSERT INTO `scoure` VALUES (3, '1003', '英语');
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`sex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`age` int(11) NOT NULL,
`sno` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '学号',
`class_num` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '班级号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 17 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '张三', '男', 20, '202001', '20201001');
INSERT INTO `student` VALUES (2, '李四', '男', 21, '202002', '20201001');
INSERT INTO `student` VALUES (3, '王五', '男', 20, '202003', '20201001');
INSERT INTO `student` VALUES (4, '张安', '女', 23, '202004', '20201001');
INSERT INTO `student` VALUES (5, '萨达', '女', 21, '202005', '20201001');
INSERT INTO `student` VALUES (6, '阿斯蒂芬', '女', 22, '202006', '20201002');
INSERT INTO `student` VALUES (7, '广大儒风', '男', 20, '202007', '20201002');
INSERT INTO `student` VALUES (8, '安顺', '男', 20, '202008', '20201002');
INSERT INTO `student` VALUES (9, '东方', '女', 20, '202009', '20201002');
INSERT INTO `student` VALUES (10, '咖啡', '男', 20, '202010', '20201002');
INSERT INTO `student` VALUES (11, '回顾', '女', 20, '202011', '20201003');
INSERT INTO `student` VALUES (12, '同意', '男', 20, '202012', '20201003');
INSERT INTO `student` VALUES (13, '规划局', '女', 20, '202013', '20201003');
INSERT INTO `student` VALUES (14, '各环节', '男', 20, '202014', '20201003');
INSERT INTO `student` VALUES (15, '空格', '女', 20, '202015', '20201003');
INSERT INTO `student` VALUES (16, '发送到', '男', 22, '202016', '20201004');
SET FOREIGN_KEY_CHECKS = 1;
1.基本查询,这里以student表为例
SELECT * FROM student;
解释:SELECT是关键字,表示将要执行一个查询,*表示“所有列”,FROM表示将要从哪个表查询
注意:
查询结果也是一个二维表,它包含列名和每一行的数据
2.条件查询
查询班级号为20201001的学生
SELECT * FROM student where class_num='20201001';
WHERE关键字后面的class_num= '20201001’就是条件。class_num是列名,该列存储了学生的班级,因此,class_num= '20201001’就筛选出了指定条件的记录。
当我们需要查询多列的时候我们需要使用’,‘来进行分割
查询班级号为20201001的学生姓名
SELECT name FROM student where class_num='20201001'
查询班级号为20201001的学生姓名和性别
SELECT name.sex FROM student where class_num='20201001;'
当我们需要以多列作为查询条件的时候我们需要使用‘or’或者‘and’来进行区分
查询班级号为20201001或者性别为男的学生性别
SELECT name FROM student where class_num = '20201001' or sex = '男';
查询班级号为 20201001 并且性别为男的学生姓名 SELECT name,sex FROM student where class_num = '20201001' and sex = '男';
NOT <条件>,表示“不符合该条件”的记录
查询班级不在 20201001 班的同学的姓名
SELECT name FROM student where not class_num = '20201001' ;
查询性别为女的同学姓名并且不在20201001班
SELECT name FROM student where sex ='女' and not class_num = '20201001';
where in的用法
IN 操作符允许我们在 WHERE 子句中规定多个值。
查询出用户id为1和3的用户记录 select * from student where id in (1,3);
3去重查询
SELECT DISTINCT sex from student
4.模糊查询
模糊查询 like 通配符(% 任意多个字符 _单个字符)
查询出所有姓王的同学
SELECT * FROM student WHERE name like '王%';
查询姓王且名字只有两个字的学生
SELECT * FROM student WHERE name like '王_';
查询出所有含有花字的同学的名称
SELECT * FROM student WHERE name like '%花%';
5.排序
我们使用SELECT查询时,细心的读者可能注意到,查询结果集通常是按照id排序的,也就是根据主键排序。这也是大部分数据库的做法。如果我们要根据其他条件排序怎么办?可以加上ORDER BY子句。例如按照年龄从低到高进行排序:
SELECT * FROM student ORDER BY age;
如果要反过来,按照年龄从高到底排序,我们可以加上DESC表示“倒序”:
SELECT * FROM student ORDER BY age DESC;
如果想按照年龄降序,并且按照学号升序怎么弄?
SELECT * FROM student ORDER BY age DESC,sno;
默认的排序规则是ASC:“升序”,即从小到大。ASC可以省略,即ORDER BY score ASC和ORDER BY score效果一样。
6.分页查询(限制查询)
使用SELECT查询时,如果结果集数据量很大,比如几万行数据,放在一个页面显示的话数据量太大,不如分页显示,每次显示100条。
要实现分页功能,实际上就是从结果集中显示第1~100条记录作为第1页,显示第101-200条记录作为第2页,以此类推。
因此,分页实际上就是从结果集中“截取”出第M~N条记录。这个查询可以通过LIMIT <M> OFFSET <N>
句子实现。我们先把所有学生按照成绩从高到低进行排序:
现在,我们把结果集分页,每页3条记录。要获取第1页的记录,可以使用LIMIT 3 OFFSET 0:
SELECT * FROM student LIMIT 3 OFFSET 0;
上述查询LIMIT 3 OFFSET 0表示,对结果集从0号记录开始,最多取3条。注意SQL记录集的索引从0开始。
如果要查询第2页,那么我们只需要“跳过”头3条记录,也就是对结果集从3号记录开始查询,把OFFSET设定为2:
SELECT * FROM student LIMIT 3 OFFSET 2;
可见,分页查询的关键在于,首先要确定每页需要显示的结果数量pageSize(这里是3),然后根据当前页的索引pageIndex(从1开始),确定LIMIT和OFFSET应该设定的值:
LIMIT总是设定为pageSize;
OFFSET计算公式为pageSize * (pageIndex - 1)。
小结
使用LIMIT <M> OFFSET <N>
可以对结果集进行分页,每次查询返回结果集的一部分;
分页查询需要先确定每页的数量和当前页数,然后确定LIMIT和OFFSET的值。