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多表聯合時,這樣對於互不關聯的數據我們也能夠一次就隻能查詢多條我們需要的數據,減少瞭請求次數一次查詢次數,而且對數據庫的性能大大提升!

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

推薦閱讀: