〇、概述

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集合运算)的更多相关文章

  1. **SQL某一表中重复某一字段重复记录查询与处理

    sql某一表中重复某一字段重复记录查询与处理   1.查询出重复记录  select 重复记录字段 form  数据表 group by houseno having count(重复记录字段)> ...

  2. hive 连接(join)查询

    1.内连接 hive> select b.*,a.name from userinfo2 b,userinfo a where a.userid=b.userid; hive> selec ...

  3. 复习MySQL④查询功能、连接方式、联合查询

    用select语句查询: select〈目标列组〉 from〈数据源〉 [where〈元组选择条件〉] [group by〈分列组〉[having 〈组选择条件〉]] [order by〈排序列1〉〈 ...

  4. SQL基础-建表

    一.建表 1.创建表的两种方式 *客户端工具 *SQL语句 2.使用SQL语句创建表 表名和字段名不能使用中文:(一般为字母开头,字母.数字.下划线组成的字符串): CREATE TABLE关键字后跟 ...

  5. MySQL全连接(Full Join)实现,union和union all用法

    MySQL本身不支持你所说的full join(全连接),但可以通过union来实现 ,下面是一个简单测试,可以看看: mysql> CREATE TABLE a(id int,name cha ...

  6. SQL基础-操纵表及插入、查询

    一.操纵表 1.表的关键信息 2.更新表名 更新表名:使用RENAME TABLE关键字.语法如下: RENAME TABLE 旧表名 TO 新表名; 比如,生产环境投产前备份teacher表,使用如 ...

  7. 转载-- SQL连接查询2 外连接(左右联接查询)

    http://www.cnblogs.com/zhangqs008/archive/2010/07/02/2341196.html 外连接主要包括左连接.右连接和完整外部连接. 1)左连接:Left ...

  8. SQL基础教程(第2版)第2章 查询基础:2-2 算数运算符和比较运算符&2-3 逻辑运算符

    ● 包含NULL的运算,其结果也是NULL. ● 判断是否为NULL,需要使用IS NULL或者IS NOT NULL运算符. ■算术运算符 ■需要注意NULL ■比较运算符 这些比较运算符可以对字符 ...

  9. SQL基础教程(第2版)第2章 查询基础:2-1 SELECT语句基础

    ● 通过指定DISTINCT可以删除重复的行.● 为列设定显示用的别名. ■列的查询 通过 SELECT 语句查询并选取出必要数据的过程称为查询(query). 该 SELECT 语句包含了 SELE ...

  10. SQL基础教程(第2版)第2章 查询基础:练习题

    SELECT product_name, regist_date FROM Product WHERE regist_date > '2009-04-28'; ① ~ ③中的 SQL 语句都无法 ...

随机推荐

  1. Elasticsearch方案选型必须了解的10件事!

    文章转载自: https://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247484372&idx=1&sn=e863e46 ...

  2. Centos7安装redash

    一.更改yum国内源: (1)cd /etc/yum.repos.d/ sudo yum install wget (2)备份:sudo mv /etc/yum.repos.d/CentOS-Base ...

  3. IDEA设置问题

    一. IDEA 相关设置 1.1 去除SQL语句的黄色背景 Settings > Editor > Inspections > SQL No data sources configu ...

  4. a除于b

    a=eval(input()) b=eval(input()) if b!=0: print("{}".format(round(a/b,2))) else: print(&quo ...

  5. 通过js实现随机生成图片

    这次给大家分享一个通过js向HTML添加便签,实现随机代码生成的案例,代码已经放在下方,这里我在下面准备了50张图片,但是没有放在博文中,如果读者想要练习,可以自己下载一些图片,建议下载的多一些. & ...

  6. 「国产系统」Tubian 0.2,兼容Windows和Android的GNU/Linux系统!

    0.3版已发布:https://www.cnblogs.com/tubentubentu/p/16733005.html Sourceforge.net主页(提供下载):https://sourcef ...

  7. 关于private子网访问s3时报错:Connect timeout on endpoint URL

    今天在使用private私有子网,通过aws s3命令访问s3时,报如下错误: [qq_5201351@private ~]$ aws s3 ls Connect timeout on endpoin ...

  8. 关于在Linux上安装aws cli版本1的记录

    AWS Command Line Interface (AWS CLI) 是一种开源工具,让您能够在命令行 Shell 中使用命令与 AWS 服务进行交互. 因公司项目要求,要在Linux服务器安装a ...

  9. 基于 Redis 实现分布式锁

    1.主流分布式锁实现方案 基于数据库实现分布式锁 基于缓存(redis 等) 基于 Zookeeper 2.根据实现方式分类 类 CAS 自旋式分布式锁:询问的方式,类似 java 并发编程中的线程获 ...

  10. faker

    faker是一个生成伪造数据的Python第三方库,可以伪造城市,姓名,文班等各自信息,而且支持中文   安装 pip3 install faker   使用 # 导包 from faker impo ...