【有道云笔记】基本sql语句2—查询基础
数据库表结构
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 course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (`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 course
-- ----------------------------
INSERT INTO `course` VALUES (1, '1001', '数学');
INSERT INTO `course` VALUES (2, '1002', '语文');
INSERT INTO `course` VALUES (3, '1003', '英语');-- ----------------------------
-- 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 = 39 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact;-- ----------------------------
-- Records of relationship
-- ----------------------------
INSERT INTO `relationship` VALUES (1, '202001', '1001');
INSERT INTO `relationship` VALUES (2, '202001', '1002');
INSERT INTO `relationship` VALUES (3, '202001', '1003');
INSERT INTO `relationship` VALUES (4, '202002', '1001');
INSERT INTO `relationship` VALUES (5, '202002', '1002');
INSERT INTO `relationship` VALUES (6, '202003', '1003');
INSERT INTO `relationship` VALUES (7, '202004', '1001');
INSERT INTO `relationship` VALUES (8, '202004', '1002');
INSERT INTO `relationship` VALUES (9, '202004', '1003');
INSERT INTO `relationship` VALUES (10, '202005', '1001');
INSERT INTO `relationship` VALUES (11, '202005', '1002');
INSERT INTO `relationship` VALUES (12, '202006', '1003');
INSERT INTO `relationship` VALUES (13, '202006', '1001');
INSERT INTO `relationship` VALUES (14, '202006', '1002');
INSERT INTO `relationship` VALUES (15, '202007', '1003');
INSERT INTO `relationship` VALUES (16, '202009', '1001');
INSERT INTO `relationship` VALUES (17, '202009', '1002');
INSERT INTO `relationship` VALUES (18, '202009', '1003');
INSERT INTO `relationship` VALUES (19, '202010', '1001');
INSERT INTO `relationship` VALUES (20, '202010', '1002');
INSERT INTO `relationship` VALUES (21, '202010', '1003');
INSERT INTO `relationship` VALUES (22, '202011', '1001');
INSERT INTO `relationship` VALUES (23, '202012', '1002');
INSERT INTO `relationship` VALUES (24, '202012', '1003');
INSERT INTO `relationship` VALUES (25, '202013', '1001');
INSERT INTO `relationship` VALUES (26, '202013', '1002');
INSERT INTO `relationship` VALUES (27, '202014', '1003');
INSERT INTO `relationship` VALUES (28, '202014', '1001');
INSERT INTO `relationship` VALUES (29, '202014', '1002');
INSERT INTO `relationship` VALUES (30, '202015', '1003');
INSERT INTO `relationship` VALUES (31, '202015', '1001');
INSERT INTO `relationship` VALUES (32, '202016', '1002');
INSERT INTO `relationship` VALUES (33, '202016', '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.查询学生的姓名
select name from student
2.查询学生姓名和性别
select name,sex from student
3.查询学生全部信息
select * from student
SELECT是关键字,表示将要执行一个查询,* 表示“所有列”,FROM表示将要从哪个表查询
注意:查询结果也是一个二维表,它包含列名和每一行的数据
条件查询
1.查询班级号为 20201001 的学生
SELECT * FROM student where class_num = '20201001';
2.查询年龄为20岁的学生
SELECT name FROM student where age = 20
3.查询班级号为 20201001 的学生姓名和性别 当我们需要查询多列的时候我们需要使用 ‘,’ 来进行分割
SELECT name,sex FROM student where class_num = '20201001';
4.查询出年龄不是20岁的学生
SELECT name FROM student where age <> 20
SELECT name FROM student where age != 20
5.查询出年龄小于21岁的学生
SELECT * FROM student where age < 21
6.查询出年龄在21-25岁的学生
SELECT * FROM student where age between 21 and 25
7.查询出班级号为空的同学名称
SELECT * FROM student where class_num is null
8.查询出班级号为20201001班的男同学
SELECT * FROM student where class_num = '20201001' and sex = '男'
9.查询出班级号为20201001班和20201002班的同学
SELECT * FROM student where class_num = '20201001' or class_num = '20201002'
10.查询出查询出用户id为1和3的用户记录 IN 操作符允许我们在 WHERE 子句中规定多个值。
select * from student where id in (1,3)
11.查询出所有姓王的同学 模糊查询 like 通配符(% 任意多个字符 _单个字符)
SELECT * FROM student WHERE name like '王%';
12.查询姓王且名字只有两个字的学生
SELECT * FROM student WHERE name like '王_';
13.查询出所有含有花子的同学的名称
SELECT * FROM student WHERE name like '%花%';
分组函数/聚合函数
1.求学生年龄的总和
select sum(age) from student;
2.查询出学生的平均年龄
select avg(age) from student;
3.查询出年龄最大的同学
SELECT max(age) from student
4.查询所有的学生数量
SELECT count(*) from student
5.查询出班级号不为空的学生数
SELECT count(class_num) from student
注意:1、count(*)表示取得当前查询表所有记录
2、count(字段名称),不会统计为null的记录
分组查询:group by
作用:通过那个或那些字段进行分组
用法:group by 字段名称
6.求这个班的平均年龄
SELECT avg(age),class_num from student GROUP BY class_num
7.查询出各个班年龄的最大值
SELECT max(age),class_num from student GROUP BY class_num
排序
我们使用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效果一样。
分页查询(限制查询)
使用SELECT查询时,如果结果集数据量很大,比如几万行数据,放在一个页面显示的话数据量太大,不如分页显示,每次显示100条。
要实现分页功能,实际上就是从结果集中显示第1100条记录作为第1页,显示第101200条记录作为第2页,以此类推。
因此,分页实际上就是从结果集中“截取”出第M~N条记录。这个查询可以通过LIMIT OFFSET 子句实现。我们先把所有学生按照成绩从高到低进行排序:
现在,我们把结果集分页,每页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设定为3:
SELECT * FROM student LIMIT 3 OFFSET 3;
可见,分页查询的关键在于,首先要确定每页需要显示的结果数量pageSize(这里是3),然后根据当前页的索引pageIndex(从1开始),确定LIMIT和OFFSET应该设定的值:
- LIMIT总是设定为pageSize
- OFFSET计算公式为pageSize * (pageIndex - 1)
小结
使用LIMIT OFFSET 可以对结果集进行分页,每次查询返回结果集的一部分;
分页查询需要先确定每页的数量和当前页数,然后确定LIMIT和OFFSET的值。
链表查询/跨表查询
在实际开发中,数据往往不是存放一张表中,而是同时存储在多张表中,这些表与表存在着关系,我们在检索数据的时候往往需要多张表联合起来检索,这种多表联合检索被称为连表查询或跨表查询。
笛卡尔积现象
含义:若两张表进行连接查询的时候没有任何条件限制,最终的查询结果总数是两张表记录的成绩,该现象称为笛卡尔积现象。
1.显示每个学生信息,并显示所属班级名称
SELECT s.name,c.class_name from student s,class c
连接查询根据:年代分类
1.SQL92语法 select xxx from A 表名,B表名 where 表连接条件 and 数据查询条件;
SELECT s.name,c.class_name from student s,class c where s.class_num = c.class_num
缺点:表连接条件与查询条件放在一起,没有分离
2.SQL99语法 select xxx from A 表名 join B 表名 on 表的连接条件;
select student.name,class.class_name from student join class on student.class_num = class.class_num;
优点:表连接独立,结构清晰,如果结果数据不满足要求,可再追加where条件进行过滤;
连接查询根据:连接方式分类。
连接查询根据:连接方式分类
1.内连接
SELECT student.name,class.class_name from student
INNER JOIN class on student.class_num = class.class_num;
2.左外连接 包含左边表的全部行(不管右边的表中是否存在与他们匹配的行),以及右边表中全部匹配的行。
SELECT student.name,class.class_name from student
LEFT JOIN class on student.class_num = class.class_num;
3.右外连接 包含右边表的全部行(不管右边的表中是否存在与他们匹配的行),以及左边表中全部匹配的行。
SELECT student.name,class.class_name from student
RIGHT JOIN class on student.class_num = class.class_num;
查询出每个学生所选择的课程
SELECT student.name,course.gradeName from studentjoin relationship on student.sno = relationship.sno join course on relationship.cno = course.cno
多张表进行表连接得语法格式
selectxxx
fromA表
joinB表
on连接条件1
joinC表
on连接条件2
嵌套查询
一般在子查询中,程序先运行在嵌套在最内层的语句,再运行外层。因此在写子查询语句时,可以先测试下内层的子查询语句是否输出了想要的内容,再一层层往外测试,增加子查询正确率。否则多层的嵌套使语句可读性很低。
1.找到所有选选择课程号为1001的同学的名称
select t1.name FROM
(SELECT student.name,relationship.cno FROM student
INNER JOIN relationship on student.sno = relationship.sno) t1
WHERE t1.cno = '1001';
2.找到所有选选择课程号为 数学 的同学的名称
SELECT t2.name FROM
(select t1.name,scoure.gradeName from
(SELECT student.name,relationship.cno FROM student INNER JOIN relationship on student.sno = relationship.sno) t1INNER JOIN scoure on t1.cno = scoure.cno) t2where gradeName = '数学';