当前位置:网站首页>Mysql互不关联的联表查询(减少了查询的次数)
Mysql互不关联的联表查询(减少了查询的次数)
2022-07-26 22:38:00 【·~简单就好】
Mysql互不关联的联表查询(减少了查询的次数)
前言
为了减少对数据库的查询次数,例如在互不关联的表中为了减轻系统的压力,我们可以通过union all关键词将多个表查到的数据做一个联查处理
(便于统计分析时使用到不同的数据而只用一次请求)
举例:通过一条sql语句一次查询查询学生表中的性别为男的学生总数和教师表中的教师性别为男的教师总数

数据库表准备:
1、student表
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
`birth` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
`sex` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('01', '赵雷', '1990-01-01', '男');
INSERT INTO `student` VALUES ('02', '钱电', '1990-12-21', '男');
INSERT INTO `student` VALUES ('03', '孙风', '1990-05-20', '男');
INSERT INTO `student` VALUES ('04', '李云', '1990-08-06', '男');
INSERT INTO `student` VALUES ('05', '周梅', '1991-12-01', '女');
INSERT INTO `student` VALUES ('06', '吴兰', '1992-03-01', '女');
INSERT INTO `student` VALUES ('07', '郑竹', '1989-07-01', '女');
INSERT INTO `student` VALUES ('08', '王菊', '1990-01-20', '女');
SET FOREIGN_KEY_CHECKS = 1;
2、teacher表
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
`sex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('01', '张三', '男');
INSERT INTO `teacher` VALUES ('02', '李四', '女');
INSERT INTO `teacher` VALUES ('03', '王五', '男');
SET FOREIGN_KEY_CHECKS = 1;
一、传统方法(查询性能不佳)
对所查询的数据封装成一个表,在分别对表的数据查询展示出来。
这种方法比较简单但是会对数据库的查询次数大大提高
SELECT
t1.学生男生总数,
t2.男教师总数
FROM
( SELECT count( id ) AS 学生男生总数 FROM student WHERE student.sex = '男' ) t1,
( SELECT count( id ) AS 男教师总数 FROM teacher WHERE teacher.sex = '男' ) t2

二、使用union all将多个表联合成一个表查询
select t.* from
( SELECT count(id) as a,0 as b FROM student WHERE student.sex = '男'
union all
SELECT 0 as a,count(id) as b FROM teacher WHERE teacher.sex = '男' ) t
1、此时a代表学生性别为男生的总人数,b代表教师性别为男的总人数
2、此时我们只需对a和b分别求和,就能够查询出男学生和男教师的总人数
select sum(t.a) as 学生男生总数,sum(t.b) as 男教师总数 from
( SELECT count(id) as a,0 as b FROM student WHERE student.sex = '男'
union all
SELECT 0 as a,count(id) as b FROM teacher WHERE teacher.sex = '男' ) t

三、总结
当我们使用union all多表联合时,这样对于互不关联的数据我们也能够一次就只能查询多条我们需要的数据,减少了请求次数一次查询次数,而且对数据库的性能大大提升!
边栏推荐
- Oracle data guard service, process and protection mode
- Flink SQL (II) Kafka connector
- Matlab simulation of inverted pendulum control system based on qlearning reinforcement learning
- 我的第一篇博客-迷茫的大三人
- C and pointer Chapter 18 runtime environment 18.2 interface between C and assembly language
- 蒙着头配置deeplabcut 1
- CDs simulation of minimum dominating set based on MATLAB
- 寻找真凶
- 3_Jupyter Notebook, numpy和matplotlib
- C and pointer Chapter 18 runtime efficiency 18.3 runtime efficiency
猜你喜欢

c语言 static运用,灵活改变生命周期,让你写代码如鱼得水

Relationship between limit, continuity, partial derivative and total differential of multivariate function (learning notes)

Based on the theoretical principle and simulation results of MATLAB spherical decoding, compare 2norm spherical decoding, infinite norm spherical decoding, ML detection

Uni app learning (II)

Configure deeplobcut 1 with your head covered

1、 Kubernetes basic concept + environment installation (build cross server public network environment)

20220720 toss deeplobcut2

5_线性回归(Linear Regression)

2022-07-17:1, 2, 3... N-1, N, n+1, n+2... In this sequence, only one number has repetition (n). This sequence is unordered. Find the repeated number n. This sequence is ordered. Find the repeated numb

2020-12-20 九九乘法表
随机推荐
4. Talk about the famous Zhang Zhengyou calibration method
Xshell连接服务器时报“Could not load host key”错误
SSRF (server side request forgery) -- Principle & bypass & Defense
Based on the theoretical principle and simulation results of MATLAB spherical decoding, compare 2norm spherical decoding, infinite norm spherical decoding, ML detection
运算符重载
Drawing warehouse-3 (functional image)
简单的素数程序 初学者 希望码龄高的大佬可以一下
放图仓库-Tsai
CCPD data set processing (target detection and text recognition)
8_多项式回归及模型泛化(Polynomial Regression and Model Generalization)
Flink SQL (II) Kafka connector
Midge paper reading notes
Find method of web page parsing by crawler
Deeplabcut uses 1
C and pointer Chapter 18 runtime environment 18.7 problems
Configure deeplobcut2 with your head covered
Matlab based medical imaging technology filtering backprojection simulation, including direct backprojection, S-L filtering, R-L filtering, LeWitt filtering
Leetcode - linked list
RecBole使用1
Deployment of yolov5 on Jetson nano deepstream