您好,登錄后才能下訂單哦!
這篇文章主要介紹“Mysql聯表查詢的特點是什么”,在日常操作中,相信很多人在Mysql聯表查詢的特點是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”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
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
到此,關于“Mysql聯表查詢的特點是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。