首頁 > 軟體

Mysql深入瞭解聯表查詢的特點

2022-07-27 18:02:56

前言

為了減少對資料庫的查詢次數,例如在互不關聯的表中為了減輕系統的壓力,我們可以通過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多表聯合時,這樣對於互不關聯的資料我們也能夠一次就只能查詢多條我們需要的資料,減少了請求次數一次查詢次數,而且對資料庫的效能大大提升!

到此這篇關於Mysql深入瞭解聯表查詢的特點的文章就介紹到這了,更多相關Mysql聯表查詢內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com