【SQL基础】多表查询:子查询、连接查询(JOIN)、组合查询(UNION集合运算)
〇、概述
1、内容
JOIN表连接(内连接INNER JOIN/JOIN)(外连接LEFT/RIGHT (OUTER) JOIN)
集合运算-UNION联合
2、建表语句
drop table if exists `user_profile`; drop table if exists `question_practice_detail`; CREATE TABLE `user_profile` ( `id` int NOT NULL, `device_id` int NOT NULL, `gender` varchar(14) NOT NULL, `age` int , `university` varchar(32) NOT NULL, `gpa` float, `active_days_within_30` int , `question_cnt` int , `answer_cnt` int ); CREATE TABLE `question_practice_detail` ( `id` int NOT NULL, `device_id` int NOT NULL, `question_id`int NOT NULL, `result` varchar(32) NOT NULL ); INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12); INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25); INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30); INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2); INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70); INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13); INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52); INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong'); INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong'); INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong'); INSERT INTO question_practice_detail VALUES(4,6543,111,'right'); INSERT INTO question_practice_detail VALUES(5,2315,115,'right'); INSERT INTO question_practice_detail VALUES(6,2315,116,'right'); INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong'); INSERT INTO question_practice_detail VALUES(8,5432,118,'wrong'); INSERT INTO question_practice_detail VALUES(9,5432,112,'wrong'); INSERT INTO question_practice_detail VALUES(10,2131,114,'right'); INSERT INTO question_practice_detail VALUES(11,5432,113,'wrong');
一、子查询
1、浙江大学用户题目回答情况
子查询实现:
SELECT device_id, question_id, result FROM question_practice_detail WHERE device_id IN (SELECT device_id FROM user_profile WHERE university='浙江大学');
普通查询:
SELECT a.device_id, question_id, result FROM question_practice_detail a, user_profile b WHERE a.device_id=b.device_id and b.university='浙江大学' ORDER BY question_id ASC;
二、连接查询【尝试使用表连接操作】
1、统计每个学校的答过题的用户的平均答题数
内连接方式(JOIN/INNER JOIN):
SELECT university, --注意,如果两个表都有的字段,请带着表名 ROUND(COUNT(question_id)/COUNT(DISTINCT(question_practice_detail.device_id)),4) AS avg_answer_cnt FROM user_profile JOIN question_practice_detail ON user_profile.device_id=question_practice_detail.device_id GROUP BY university ORDER BY university ASC;
多表查询方式:
SELECT university, ROUND(COUNT(b.question_id)/COUNT(DISTINCT(b.device_id)),4) AS avg_answer_cnt FROM user_profile a, question_practice_detail b WHERE a.device_id=b.device_id GROUP BY university;
2、统计每个学校各难度的用户平均刷题数
内连接方式:
SELECT university, difficult_level, ROUND(COUNT(b.question_id)/COUNT(DISTINCT(b.device_id)),4) AS avg_answer_cnt FROM user_profile a INNER JOIN question_practice_detail b ON a.device_id=b.device_id INNER JOIN question_detail c ON c.question_id=b.question_id GROUP BY university,difficult_level;
多表查询方式:
SELECT university, difficult_level, ROUND(COUNT(b.question_id)/COUNT(DISTINCT(b.device_id)),4) AS avg_answer_cnt FROM user_profile a, question_practice_detail b, question_detail c WHERE a.device_id=b.device_id and b.question_id=c.question_id GROUP BY university, difficult_level;
3、统计每个用户的平均刷题数
查看参加了答题的山东大学的用户在不同难度下的平均答题题目数
内连接方式:
SELECT university, difficult_level, ROUND(COUNT(b.question_id)/COUNT(DISTINCT(b.device_id)),4) AS avg_answer_cnt FROM user_profile a INNER JOIN question_practice_detail b ON a.device_id=b.device_id INNER JOIN question_detail c ON b.question_id=c.question_id WHERE university='山东大学' GROUP BY difficult_level;
多表查询方式:
SELECT university, difficult_level, ROUND(COUNT(b.question_id)/COUNT(DISTINCT(b.device_id)),4) AS avg_answer_cnt FROM user_profile a, question_practice_detail b, question_detail c WHERE a.device_id=b.device_id and b.question_id=c.question_id and university='山东大学' GROUP BY difficult_level;
三、组合查询
1、查找山东大学或者性别为男生的信息
(注意输出的顺序,先输出学校为山东大学再输出性别为男生的信息)
错误做法:
SELECT device_id, gender, age, gpa FROM user_profile WHERE university='山东大学' or gender='male';
正确做法:【不去重用union】
SELECT device_id, gender, age, gpa FROM user_profile WHERE university='山东大学' UNION ALL SELECT device_id, gender, age, gpa FROM user_profile WHERE gender='male';
本文来自博客园,作者:哥们要飞,转载请注明原文链接:https://www.cnblogs.com/liujinhui/p/16171977.html