日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql不同字段full join_Mysql实现full join的替换方法

發布時間:2023/12/10 数据库 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql不同字段full join_Mysql实现full join的替换方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目前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_interest

from

(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

) b

group by b.the_day; 大致思路說下:2個被連接表增加一個標識字段,使用不同的值,之后將這2個表union all下,使用連接條件將union all的結果分組且使用行列轉換。

總結

以上是生活随笔為你收集整理的mysql不同字段full join_Mysql实现full join的替换方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。