题:下表是一张商品出售统计表,写一段简单的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综合题的更多相关文章

  1. MySQL查询笔试综合题练习

    题目要求: 在某个数据库下建表: create table stu( -> name char(3) not null default '', -> subject varchar(10) ...

  2. [WeChall] Training: MySQL I (MySQL, Exploit, Training)

    Training: MySQL I (MySQL, Exploit, Training) MySQL Authentication Bypass - The classic This one is t ...

  3. 涂抹mysql笔记-mysql性能调优和诊断

    <>关键性指标1.IOPS(Input/Output operations Per Second)每秒处理的I/O请求次数:需要说明的一点,通常提到磁盘读写能力,比如形容它每秒读300M写 ...

  4. MySQL: Table 'mysql.plugin' doesn't exist的解决

    安装解压版MySQL以后,不能启动,日志里面出现了这个错误: MySQL: Table 'mysql.plugin' doesn't exist 这是因为mysql服务启动时候找不到内置数据库&quo ...

  5. MySql 及 MySql WorkBench使用大全

    Mysql安装步骤 1. 下载MySQL Community Server 5.6.13 2. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下,我的解压目录是: "D:\ ...

  6. mac使用终端运行mysql,mysql终端,mysql mac,mysql目录,mysql路径

    首先去官网下载: http://www.mysql.com/downloads/ 我下载了5.6.11的dmg然后安装,安装完成之后..如果要用终端去玩SQL.那么一开始要输入很长的:/usr/loc ...

  7. yum安装mysql和mysql源,配置mysql

    申明,不要用root安装 1. 下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm ...

  8. MySQL服务 - MySQL列类型、SQL模式、数据字典

    MySQL列类型的作用: 列类型可以简单理解为用来对用户往列种存储数据时做某种范围"限定",它可以定义数据的有效值(字符.数字等).所能占据的最大存储空间.字符长度(定长或变长). ...

  9. OS 系统下安装MySql 配置MySql环境变量

    学习Hive需要,闲话不说 本文的内容: 下载Mysql for Mac 下载Mysql Workbench 安装 Mysql 和 Mysql Workbench 配置Mysql在OS 系统上的环境变 ...

  10. 详解 Spotlight on MySQL监控MySQL服务器

    前一章详解了Spotlight on Unix 监控Linux服务器 ,今天再来看看Spotlight on MySQL怎么监控MySQL服务器. 注:http://www.cnblogs.com/J ...

随机推荐

  1. python学习之老男孩python全栈第九期_day013知识点总结

    # l = [1,2,3]# 索引# 循环 for # list # dic # str # set # tuple # f = open() # range() # enumerate'''prin ...

  2. YII使用beanstalk队列

    转载于:http://blog.csdn.net/yao970953039/article/details/41821387 1.系统centos 我是直接使用yum install beanstal ...

  3. FineReport软件

    # FineReport常见问题与解答 ### FineReport是什么?FineReport,企业级web报表工具,中国报表软件知名品牌.借助于FineReport的无码理念,用户可以轻松的构建出 ...

  4. mysql 导入时报错:Got a packet bigger than‘max_allowed_packet’bytes

    原因是max_allowed_packet 值设置过小. 网上粘贴一段定义: max_allowed_packet:指代mysql服务器端和客户端在一次传送数据包的过程当中数据包的大小这个是定义mys ...

  5. css动画笔记

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. ArcGIS10+:ArcGIS version not specified. You must call RuntimeManager.Bind before creating any ArcGIS

    ArcGIS10+版本,使用VS创建一个简单的AE应用程序,然后拖放一个toolbar.LicenseControl以及MapControl控件. 接着编译应用程序,编译成功. 然后单击F5运行程序, ...

  7. GitHub已将持续集成服务器Janky开源

    GitHub已将Janky开源,这是他们构建在Jenkins之上的持续集成服务器,并在其中增加了聊天自动化工具Hubot. 除了一般的Jenkins功能之外,Janky还通过Hubot对功能进行了补充 ...

  8. CVE-2015-3864漏洞利用分析(exploit_from_google)

    title: CVE-2015-3864漏洞利用分析(exploit_from_google) author: hac425 tags: CVE-2015-3864 文件格式漏洞 categories ...

  9. 612.1.002 ALGS4 | Analysis of Algorithms

    我们生活在大数的时代 培养数量级的敏感! Tip:见招拆招 作为工程师,你先要能实现出来. 充实基础,没有什么不好意思 哪怕不完美.但是有时候完成比完美更重要. 之后再去想优化 P.S.作者Rober ...

  10. redis HyperLogLog 基数估算

    HyperLogLog 可以接受多个元素的输入,返回输入元素的基数估算值基数,集合中不同元素的数量.如集合{1,2,3,1,2,3,4}的基数是4.估算,HyperLogLog算法返回的基数不是完全精 ...