如何将一张表中的数据迁移到另一张表中,或者是将多张表的数据迁移到一张表中

出现这个需求的原因是因为项目里面有个订单迁移工程,某一天的有些订单没有迁移到历史表里面去,然后现在要我将这些订单重新迁移到历史表中,

先写一个简单的例子,比如说我现在B表中的数据,要迁移到A表中,

可以直接这么写,

insert into A(字段1,字段2,字段3......) select 字段1,字段2,字段3.... from B

注意字段一定要对上,不然到时订单字段迁移错误就尴尬了,写完后,你可能会报个“ORA-00913: 值过多”的错误,不要担心,是你字段写错误了,一个一个重新对比一下,

再举个“栗子”,

如果现在要将B表和C表的数据都迁移到A表中

insert into A(字段1,字段2,字段3,字段4) select b.字段1,b.字段2,c.字段3,c.字段4 from B b,C c where b.某字段=c.某字段

废话不多说了,我就把我的sql拿上来了

--从今日主订单表中查询数据插入历史主订单表中,
insert into qypt_summary_payment(
account,app_no,channel_no,form_id,form_time,
good_price,order_no,pay_price,pay_status,register_name,
register_phone,installment_mode,installment_number,installment_rate,discount_rate,
Unionpay_Pay_Status,installment_id,discount_id,consume_posi,certificate_no,
user_id,certificate_type,channel_order_no,cashier_no,attach,
point,point_flag,assist_pay_flag
)
select 
account,app_no,channel_no,id,form_time,
form_price,form_id,Actually_Form_Price,pay_status,register_name,
register_phone,installment_mode,installment_number,installment_rate,
discount_rate,Unionpay_Pay_Status,installment_id,discount_id,consume_posi,
certificate_no,user_id,certificate_type,channel_order_no,cashier_no,
attach,point,point_flag,assist_pay_flag 
from qypt_pay_history_20180329
--从今日主订单表中查询数据插入历史子订单表中,
insert into qypt_summary_payment_detail(
app_no,cards,channel_no,form_id,form_price,pay_price,
industry_no,mobile,name,username,installment_mode,installment_number,
discount_rate,unionpay_pay_status,installment_id,discount_id,consume_posi,
certificate_no,user_id,
certificate_type,channel_order_no,cashier_no,attach,point,point_flag,
form_time,good_id,good_price,good_pay_price,good_type,market_no,
order_no,pay_status,
return_status,sub_order_no,sub_point,reject_no
)
select 
a.app_no,a.account,channel_no,b.form_id,a.form_price,a.actually_form_price,a.Industry_no,
a.register_phone,a.register_name,a.register_name,a.installment_mode,a.installment_number,a.discount_rate,
a.unionpay_pay_status,a.installment_id,a.discount_id,a.consume_posi,a.certificate_no,a.user_id,
a.certificate_type,a.channel_order_no,a.cashier_no,a.attach,a.point,a.point_flag,

b.form_time,b.good_id,b.price,b.actually_price,b.good_type,b.actcode,
b.order_no,b.pay_status,
b.return_status,b.sub_order_no,
b.point,b.reject_no

from qypt_pay_history_20180329 a,qypt_pay_history_sub_20180329 b where a.form_id = b.order_no  and a.app_no='Y00101'

猜你喜欢

转载自blog.csdn.net/qq_35868412/article/details/80054684
今日推荐