Mysql实现full join的替换方法
来源:互联网 发布:淘宝地区编码怎么填 编辑:程序博客网 时间:2024/06/10 07:25
目前mysql还不支持full join,只能使用left join、union、right join来实现。但使用这个方法解决多次full join的话代码量非常庞大,一直在思考有没有其他替代方法。
今天解决一个问题的时候突然想到了一个替代方法:使用行列转换。
这个方法有一定的局限性,就是连接条件在2个连接表中最多只能有一条记录(不过大部分连接情况也就是连接条件在连接表中最多只有一条记录)。
下面是我具体实现的sql:
select b.the_day, max(if(b.type_id = 1, b.amount, 0)) purchase_amount, max(if(b.type_id = 1, b.interest, 0)) purchase_interest, max(if(b.type_id = 2, b.amount, 0)) withdrawal_amount, max(if(b.type_id = 2, b.interest, 0)) withdrawal_interestfrom (select a.the_day, round(max(if(a.type_id = 1, a.money, 0))) amount, max(if(a.type_id = 2, a.money, 0)) interest, 1 type_id from (select date(pe.purchase_begin_time) the_day, sum(pe.amount) money, 1 type_id from purchase pe inner join product pt on pe.product_id = pt.product_id and pt.type_id = 9 where pe.purchase_status_id = 4 and pe.purchase_begin_time < '2015-08-13 21:00:00' group by the_day union all select date(uci.creat_time) the_day, sum(uci.interest) money, 2 type_id from user_current_interest uci where uci.type = 'IN' and uci.creat_time < '2015-08-14' group by the_day ) a group by a.the_day union all select date(wd.withdrawal_begin_time) the_day, sum(wd.amount), sum(wd.profit), 2 type_id from withdrawal wd where wd.withdrawal_status_id in (4, 5) and wd.product_id = 0 and wd.withdrawal_begin_time < '2015-08-14' group by the_day ) bgroup by b.the_day;
大致思路说下:2个被连接表增加一个标识字段,使用不同的值,之后将这2个表union all下,使用连接条件将union all的结果分组且使用行列转换。
0 0
- Mysql实现full join的替换方法
- MySQL Full Join的实现
- mysql的full join的实现
- Mysql实现全连接FULL OUTER JOIN
- MySQL全连接(Full Join)实现
- Mysql实现全连接FULL OUTER JOIN
- mysql 里怎么实现full join
- mysql中实现oracle中的full join
- mysql full join 解决方案
- MySQL不支持FULL JOIN, INTERSECT和MINUS(except)的替代方法
- MySQL中LEFT JOIN 、RIGHT JOIN、INNER JOIN、FULL JOIN 的区别
- Full JOIN 的定义
- mysql join, left join, right join, full join from w3chool
- LINQ full out join 实现
- LINQ full out join 实现
- sql的inner join/left join/right join/full join
- inner join, left join, right join, full join 的区别
- inner join, left join, right join, full join 的区别
- C++ 内存管理
- Mysql事务提交方式
- n*n阶矩阵逆时针输出
- 【云快讯】《“云计算”也要防雷,20%数据中心故障和雷击有关》
- maven 修改使用jdk版本
- Mysql实现full join的替换方法
- python字符串format函数学习
- Android应用Preference相关及源码浅析(Preference组件家族篇)
- 状态栏通知Notification、NotificationManager详解
- Centos简单搭建SVN服务器三步曲
- resources dependent
- Android 自定义控件 轻松实现360软件详情页
- OC中NSString 用法
- POJ2151Check the difficulty of problems【概率dp求概率】