首頁 >資料庫 >mysql教程 >Mysql聯表查詢的特點是什麼

Mysql聯表查詢的特點是什麼

WBOY
WBOY轉載
2023-05-29 11:28:051720瀏覽

前言

為了減少對資料庫的查詢次數,例如在互不關聯的表中為了減輕系統的壓力,我們可以透過union all關鍵字將多個表查到的資料做一個聯查處理

(便於統計分析時使用到不同的資料而只用一次請求)

範例:透過一個sql語句一次查詢查詢學生表中的性別為男的學生總數和教師表中的教師性別為男的教師總數

Mysql聯表查詢的特點是什麼

Mysql聯表查詢的特點是什麼

#資料庫表準備:

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

Mysql聯表查詢的特點是什麼

#二、使用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代表教師性別為男的總人數

Mysql聯表查詢的特點是什麼

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

Mysql聯表查詢的特點是什麼

以上是Mysql聯表查詢的特點是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除