【MySQL】一道MySQL综合题
题:下表是一张商品出售统计表,写一段简单的sql查询,查询出每种商品类型每个月的出售总额,其中类型1为实体商品,类型2为虚拟商品。表名goods_count
。
id(自增id) | sold_time(出售时间戳) | amount(价格) | goods_type(商品类型) |
---|---|---|---|
1 | 1425265920 | 23.50 | 2 |
2 | 1428203520 | 50.00 | 1 |
3 | 1430709120 | 100.00 | 1 |
4 | 1430795520 | 65.25 | 1 |
5 | 1431659520 | 255.20 | 2 |
要求打印如下结果:
月份 | 实体商品 | 虚拟商品 |
---|---|---|
2015-03 | 0.00 | 23.50 |
2015-04 | 50.00 | 0.00 |
2015-05 | 162.25 | 255.20 |
模拟
create DATABASE `test`;
CREATE TABLE `goods_count`(
`id` int AUTO_INCREMENT PRIMARY KEY ,
`sold_time` int ,
`amount` FLOAT,
`goods_type` TINYINT
);
INSERT INTO `goods_count`(sold_time,amount,goods_type) VALUES(1425265920,23.50 ,2);
INSERT INTO `goods_count`(sold_time,amount,goods_type) VALUES(1428203520,50.00 ,1);
INSERT INTO `goods_count`(sold_time,amount,goods_type) VALUES(1430709120,100.00 ,1);
INSERT INTO `goods_count`(sold_time,amount,goods_type) VALUES(1430795520,65.25 ,1);
INSERT INTO `goods_count`(sold_time,amount,goods_type) VALUES(1431659520,255.20 ,2);
分析:
(1) 按月获取实体商品的出售总额
SELECT ALL from_unixtime(sold_time, '%Y-%m') as m, IF(sum(amount) IS NULL , 0, round(sum(amount),2) ) as s
FROM `goods_count`
WHERE goods_type=1
GROUP BY m
(2)按月获取虚拟商品的出售总额
SELECT ALL from_unixtime(sold_time, '%Y-%m') as m, IF(sum(amount) IS NULL , 0, round(sum(amount),2) ) as s
FROM `goods_count`
WHERE goods_type=2
GROUP BY m
(3)现在的问题是如何将两个表连接在一起?
- 左连接:
SELECT ALL t1.m as '月份', IF(t1.s IS NULL , 0, t1.s) as '实体商品', IF(t2.s IS NULL , 0, t2.s) as '虚拟商品'
FROM (
SELECT ALL from_unixtime(sold_time, '%Y-%m') as m, round(sum(amount),2) as s
FROM `goods_count`
WHERE goods_type=1
GROUP BY m
) as t1
LEFT JOIN
(
SELECT ALL from_unixtime(sold_time, '%Y-%m') as m, round(sum(amount),2) as s
FROM `goods_count`
WHERE goods_type=2
GROUP BY m
) as t2
ON t1.m = t2.m;
- 右连接:
SELECT ALL t1.m as '月份', IF(t1.s IS NULL , 0, t1.s) as '实体商品', IF(t2.s IS NULL , 0, t2.s) as '虚拟商品'
FROM (
SELECT ALL from_unixtime(sold_time, '%Y-%m') as m, round(sum(amount),2) as s
FROM `goods_count`
WHERE goods_type=1
GROUP BY m
) as t1
RIGHT JOIN
(
SELECT ALL from_unixtime(sold_time, '%Y-%m') as m, round(sum(amount),2) as s
FROM `goods_count`
WHERE goods_type=2
GROUP BY m
) as t2
ON t1.m = t2.m;
- 右连接优化:
SELECT ALLIF( t1.m IS NULL, t2.m, t1.m) as '月份', IF(t1.s IS NULL , 0, t1.s) as '实体商品', IF(t2.s IS NULL , 0, t2.s) as '虚拟商品'
FROM (
SELECT ALL from_unixtime(sold_time, '%Y-%m') as m, round(sum(amount),2) as s
FROM `goods_count`
WHERE goods_type=1
GROUP BY m
) as t1
RIGHT JOIN
(
SELECT ALL from_unixtime(sold_time, '%Y-%m') as m, round(sum(amount),2) as s
FROM `goods_count`
WHERE goods_type=2
GROUP BY m
) as t2
ON t1.m = t2.m;
- 思考:
此处的主要问题在于ON
的连接条件,导致不能将未对应的月份显示出来。
最后只想到了一个最暴力的方法==》三表连接
SELECT ALL a.m as '月份', IF(t1.s IS NULL , 0, t1.s) as '实体商品', IF(t2.s IS NULL , 0, t2.s) as '虚拟商品'
FROM
(select from_unixtime(sold_time, '%Y-%m') as m
from goods_count
group by m
) as a
left join (
SELECT ALL from_unixtime(sold_time, '%Y-%m') as m, round(sum(amount),2) as s
FROM `goods_count`
WHERE goods_type=1
GROUP BY m
) as t1
on a.m = t1.m
left JOIN
(
SELECT ALL from_unixtime(sold_time, '%Y-%m') as m, round(sum(amount),2) as s
FROM `goods_count`
WHERE goods_type=2
GROUP BY m
) as t2
on a.m =t2.m;
【MySQL】一道MySQL综合题的更多相关文章
- MySQL查询笔试综合题练习
题目要求: 在某个数据库下建表: create table stu( -> name char(3) not null default '', -> subject varchar(10) ...
- [WeChall] Training: MySQL I (MySQL, Exploit, Training)
Training: MySQL I (MySQL, Exploit, Training) MySQL Authentication Bypass - The classic This one is t ...
- 涂抹mysql笔记-mysql性能调优和诊断
<>关键性指标1.IOPS(Input/Output operations Per Second)每秒处理的I/O请求次数:需要说明的一点,通常提到磁盘读写能力,比如形容它每秒读300M写 ...
- MySQL: Table 'mysql.plugin' doesn't exist的解决
安装解压版MySQL以后,不能启动,日志里面出现了这个错误: MySQL: Table 'mysql.plugin' doesn't exist 这是因为mysql服务启动时候找不到内置数据库&quo ...
- MySql 及 MySql WorkBench使用大全
Mysql安装步骤 1. 下载MySQL Community Server 5.6.13 2. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下,我的解压目录是: "D:\ ...
- mac使用终端运行mysql,mysql终端,mysql mac,mysql目录,mysql路径
首先去官网下载: http://www.mysql.com/downloads/ 我下载了5.6.11的dmg然后安装,安装完成之后..如果要用终端去玩SQL.那么一开始要输入很长的:/usr/loc ...
- yum安装mysql和mysql源,配置mysql
申明,不要用root安装 1. 下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm ...
- MySQL服务 - MySQL列类型、SQL模式、数据字典
MySQL列类型的作用: 列类型可以简单理解为用来对用户往列种存储数据时做某种范围"限定",它可以定义数据的有效值(字符.数字等).所能占据的最大存储空间.字符长度(定长或变长). ...
- OS 系统下安装MySql 配置MySql环境变量
学习Hive需要,闲话不说 本文的内容: 下载Mysql for Mac 下载Mysql Workbench 安装 Mysql 和 Mysql Workbench 配置Mysql在OS 系统上的环境变 ...
- 详解 Spotlight on MySQL监控MySQL服务器
前一章详解了Spotlight on Unix 监控Linux服务器 ,今天再来看看Spotlight on MySQL怎么监控MySQL服务器. 注:http://www.cnblogs.com/J ...
随机推荐
- Maven 使用 Nexus 内部库 代理
反正任由总理怎么强调,在中国的当前的网络环境下,中央库的访问速度总是令人心碎.建一个nexus内部库可以建立缓存,只要有人通过它下载了相关的maven依赖,那么别人需要时可以马上从本地网络的服务器上返 ...
- ArcGIS10.3+Oracle12C+ArcGIS Server10.3安装布署(之三)
1.将Oracle的客户端切换到64位 (1)将C:\下的instantclient_12_1目录重命名为instantclient_12_1X86 (2)从Oracle的官方网站下载 insta ...
- FineReport中如何自定义登录界面
在登录平台时,不希望使用FR默认的内置登录界面,想通过自定义登录界面实现登录操作,内置登录界面如下图: 登录界面,获取到用户名和密码的值,发送到报表系统,报表服务带着这两个参数访问认证地址进行认证. ...
- INFO: Font Metrics and the Use of Negative lfHeight
INFO: Font Metrics and the Use of Negative lfHeight Print Email Article translations Article ID ...
- Phoenix介绍(持续更新)
现有hbase的查询工具有很多如:Hive,Tez,Impala,Shark/Spark,Phoenix等.今天主要记录Phoenix. phoenix,中文译为“凤凰”,很美的名字.Phoenix是 ...
- unity3d中的自定义模型的顶点法线和建模软件中的术语“软硬边”和立方体
在unity3d中我是想用Mesh生成一个正方体,直到遇到了法线的问题. 我是想显示如下图所示的正方体,却发现法线设置上的问题. 这里我先使用了8个顶点 按照每个顶点一个法线的结果,只能是这样:(也就 ...
- Java设计模式----观察者模式详解
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
- js判断状态
'<input type="radio" class="danxuan" name="danxuan" code="'||v ...
- 转:Eclipse+webservice开发实例
原文地址:http://blog.csdn.net/xw13106209/article/details/7049614 1.参考文献: 1.利用Java编写简单的WebService实例 http ...
- Django的model中创建表
类中的class Meta字段的作用: 第一个作用可以给这个类起名字 在后台的admin中显示这个类名字 class CourseCategory(models.Model): "" ...