【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';
【SQL基础】多表查询:子查询、连接查询(JOIN)、组合查询(UNION集合运算)的更多相关文章
- **SQL某一表中重复某一字段重复记录查询与处理
sql某一表中重复某一字段重复记录查询与处理 1.查询出重复记录 select 重复记录字段 form 数据表 group by houseno having count(重复记录字段)> ...
- hive 连接(join)查询
1.内连接 hive> select b.*,a.name from userinfo2 b,userinfo a where a.userid=b.userid; hive> selec ...
- 复习MySQL④查询功能、连接方式、联合查询
用select语句查询: select〈目标列组〉 from〈数据源〉 [where〈元组选择条件〉] [group by〈分列组〉[having 〈组选择条件〉]] [order by〈排序列1〉〈 ...
- SQL基础-建表
一.建表 1.创建表的两种方式 *客户端工具 *SQL语句 2.使用SQL语句创建表 表名和字段名不能使用中文:(一般为字母开头,字母.数字.下划线组成的字符串): CREATE TABLE关键字后跟 ...
- MySQL全连接(Full Join)实现,union和union all用法
MySQL本身不支持你所说的full join(全连接),但可以通过union来实现 ,下面是一个简单测试,可以看看: mysql> CREATE TABLE a(id int,name cha ...
- SQL基础-操纵表及插入、查询
一.操纵表 1.表的关键信息 2.更新表名 更新表名:使用RENAME TABLE关键字.语法如下: RENAME TABLE 旧表名 TO 新表名; 比如,生产环境投产前备份teacher表,使用如 ...
- 转载-- SQL连接查询2 外连接(左右联接查询)
http://www.cnblogs.com/zhangqs008/archive/2010/07/02/2341196.html 外连接主要包括左连接.右连接和完整外部连接. 1)左连接:Left ...
- SQL基础教程(第2版)第2章 查询基础:2-2 算数运算符和比较运算符&2-3 逻辑运算符
● 包含NULL的运算,其结果也是NULL. ● 判断是否为NULL,需要使用IS NULL或者IS NOT NULL运算符. ■算术运算符 ■需要注意NULL ■比较运算符 这些比较运算符可以对字符 ...
- SQL基础教程(第2版)第2章 查询基础:2-1 SELECT语句基础
● 通过指定DISTINCT可以删除重复的行.● 为列设定显示用的别名. ■列的查询 通过 SELECT 语句查询并选取出必要数据的过程称为查询(query). 该 SELECT 语句包含了 SELE ...
- SQL基础教程(第2版)第2章 查询基础:练习题
SELECT product_name, regist_date FROM Product WHERE regist_date > '2009-04-28'; ① ~ ③中的 SQL 语句都无法 ...
随机推荐
- NLP新手入门指南|北大-TANGENT
开源的学习资源:<NLP 新手入门指南>,项目作者为北京大学 TANGENT 实验室成员. 该指南主要提供了 NLP 学习入门引导.常见任务的开发实现.各大技术教程与文献的相关推荐等内容, ...
- Elasticsearch 索引生命周期管理 ILM 实战指南
文章转载自:https://mp.weixin.qq.com/s/7VQd5sKt_PH56PFnCrUOHQ 1.什么是索引生命周期 在基于日志.指标.实时时间序列的大型系统中,集群的索引也具备类似 ...
- Traefik2.3.x 使用大全(更新版)
文章转载自:https://mp.weixin.qq.com/s?__biz=MzU4MjQ0MTU4Ng==&mid=2247488793&idx=1&sn=bb2b0ad1 ...
- Traefik 2.0 实现自动化 HTTPS
文章转载自:https://mp.weixin.qq.com/s?__biz=MzU4MjQ0MTU4Ng==&mid=2247484457&idx=1&sn=35112e98 ...
- 动态存储管理实战:GlusterFS
文件转载自:https://www.orchome.com/1284 本节以GlusterFS为例,从定义StorageClass.创建GlusterFS和Heketi服务.用户申请PVC到创建Pod ...
- 通用 HTTP 签名组件的另类实现
1.初衷 开发中经常需要做一些接口的签名生成和校验工作,最开始的时候都是每个接口去按照约定单独实现,久而久之就变的非常难维护,因此就琢磨怎么能够写了一个比较通用的签名生成工具. 2.思路 采用链式调用 ...
- 微信小程序分享好友,朋友圈
<template> <view> <button open-type="share">发送给好友</button> </vi ...
- LcdTools如何导出内置画面为bmp图片
运行LcdTools,先设置好图片所需分辨率参数,点击"画面设置"栏,修改下图所示参数 点击"画面设置"栏,在"画面资源"栏找到需要导出的画 ...
- 三、Python语法介绍
三.Python语言介绍 3.1.了解Python语言 Python 是1989 年荷兰人 Guido van Rossum (简称 Guido)在圣诞节期间为了打发时间,发明的一门面向对象的解释性编 ...
- 2022icpc新疆省赛
菜鸡第一次打icpc 记录一下历程 习惯#define int long long 以下皆是按照我认为的难易顺序排序 K. 看题意大概就是说求从L加到R 1 ios::sync_with_stdio( ...