〇、概述

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. Docker与Containerd使用区别

    文章转载自:https://cloud.tencent.com/developer/article/1984040 Kubernetes 在 1.24 版本里弃用并移除 docker shim,这导致 ...

  2. 使用 Elastic 技术栈构建 K8S 全栈监控 -1:搭建 ElasticSearch 集群环境

    文章转载自:https://www.qikqiak.com/post/k8s-monitor-use-elastic-stack-1/ 操作步骤 kubectl create ns elastic k ...

  3. 通过Thread Pool Executor类解析线程池执行任务的核心流程

    摘要:ThreadPoolExecutor是Java线程池中最核心的类之一,它能够保证线程池按照正常的业务逻辑执行任务,并通过原子方式更新线程池每个阶段的状态. 本文分享自华为云社区<[高并发] ...

  4. Linux Subsystem For Android 11!适用于Debian GNU/Linux的Android子系统,完美兼容ARM安卓软件!

    本文将讲述如何在Debian Stable 系统安装一个Android 11子系统,并且这个子系统带有Houdini可以兼容专为移动设备开发的ARM软件.在root权限下,编辑/etc/apt/sou ...

  5. 云原生强大且灵活的持续集成CI开源框架Tekton实战-上

    @ 目录 概述 定义 常见CICD工具 使用好处 组件 基本概念 安装 前提条件 安装Tekton Pipelines 创建并运行任务 安装Dashboard 安装Cli Pipelines示例演示 ...

  6. PAT甲级英语单词整理

    proper 正确 合适 vertex(vertices)顶点 respectively 个别 分别 indices 指标 索引 shipping 运输 incompatible 不相容 oxidiz ...

  7. MatrixOne从入门到实践01——初识MatrixOne

    初识MatrixOne 简介 MatrixOrigin 矩阵起源 是一家数据智能领域的创新企业,其愿景是成为数字世界的核心技术提供者. 物理世界的数字化和智能化无处不在.我们致力于建设开放的技术开源社 ...

  8. Can not set int field xxx to java.lang.Long 错误

    Can not set int field xxx to java.lang.Long 错误 这个错误其实是因为Java程序和MySQL表中字段的属性匹配不一致 我的报错是Can not set ja ...

  9. String类型变量的使用

    1.String属于引用数据类型,翻译为:字符串 2.声明String类型变量时,使用一对"" 3.String可以和8种基本数据类型变量做运算,且运算只能是连接运算:+ 4.运算 ...

  10. uoj316【NOI2017】泳池

    题目链接 \(S=k\)可以拆成\(S\le k\)减去\(S\le k-1\).用\((i,j)\)表示第i行第j列. 设\(g(i,j)\)表示前i行前j列都安全其他未知满足条件的概率,\(h(i ...