表A修改表B資料功能實現

2021-06-14 10:24:48 字數 2889 閱讀 4973

今天因為原來專案方法的問題導致計算資料錯誤,現在需要將以前的資料更改回來,需要更改的表如下:

表a (學員培訓資訊表)

stus_id           integer not null,

stu_idcard varchar2(50) not null,

mas_idcard varchar2(30),

stus_starttime date not null,

stus_endtime date,

stus_totalminute integer,

stus_totalmileage integer,

stus_sinfoid integer not null,

stus_checktype varchar2(50),

stus_isok varchar2(30) default 1,

stus_carid integer,

stus_deviceid varchar2(50) not null,

ds_id integer,

consumetypeid integer default 0 not null,

createdate date,

sumofconsumption number(18,2),

createby integer,

stus_isnormal integer default 1,

stus_errorcausa varchar2(100),

region_ratio number default 0 not null,

region_min_ratio number default 0 not null,

options varchar2(200),

stus_ispass integer default 1,

stus_remark varchar2(1000),

regionids varchar2(1000)

表 b(學員學時表)

stut_id            integer not null,

stu_idcard varchar2(30) not null,

stut_sinfoid integer not null,

stut_totalperiod integer not null,

stut_lasttime date not null,

stut_totalmileage integer not null,

error_totalperiod integer default 0 not null,

error_totalmileage integer default 0 not null,

stut_nums integer default 0 not null

要根據表a中的身份證(stu_idcard)、專案編號(stus_sinfoid)來進行累加stus_totalmileage得到結果更改表b對應列值中的stut_totalmileage

首先查詢所要修改的表a資料:

select  a.stu_idcard,a.stus_sinfoid,sum(a.stus_totalmileage)  from a group by a. stu_idcard,a.stus_sinfoid order by stu_idcard

根據上面得出的資料更改表b 的stut_totalmileage

這個函式只能在9i及以上的版本使用。

說明:

在進行sql語句編寫時,我們經常會遇到大量的同時進行insert/update的語句 ,也就是說當存在記錄時,就更新(update),不存在資料時,就插入(insert)。

語法:

merge into table_name alias1 

using (table|view|sub_query) alias2

on (join condition)

when matched then

update table_name

set col1 = col_val1,

col2 = col2_val

when not matched then

insert (column_list) values (column_values)

[where col1...];

按照上面的語法邏輯就可得出下面sql語句:

merge into t_ds_stutime me 

using

(select * from (select ec.stu_idcard,ec.stus_sinfoid,sum(ec.stus_totalmileage) cc

from t_ds_stagerec ec group by ec.stu_idcard,ec.stus_sinfoid order by ec.stu_idcard)) kk

on(me.stu_idcard=kk.stu_idcard and me.stut_sinfoid=kk.stus_sinfoid )

when matched then

update set me.stut_totalmileage=kk.cc

where me.stut_totalmileage!=kk.cc

表A修改表B資料功能實現

表a修改表b資料功能實現 今天因為原來專案方法的問題導致計算資料錯誤,現在需要將以前的資料更改回來,需要更改的表如下 表a 學員培訓資訊表 sql stus id integer not null,stu idcard varchar2 50 not null,mas idcard varchar2...

插入資料a表到b表

insert into p web p p.tid,p.title,p.fileurl,p.columnid,p.columnname select l.tid,l.linkname,l.linkurl,3033 as columnid from p link l where l.columnid ...

Mysql A表 資料更新 B表

1.下面這個語句會拷貝表結構到新錶newadmin中。不會拷貝表中的資料 create table newadmin like admin 2.下面這個語句會拷貝資料到新錶中。注意 這個語句其實只是把select語句的結果建乙個表。所以newadmin這個表不會有主鍵,索引。create table...