通过读取两个表中的多个字段来更新字段

ukxgm1gy  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(335)

在mysql社区5.7db上有两个表t1和t2
t1:(prodid varchar,其他列int)

ProdID   Request   Available   Stock
------------------------------------
Prod1      17         50        33
Prod2      16         40        24
Prod43      0         10        10

t2:(compid varchar,其他列int)

CompID   Prod1   Prod2   Prod43   Request   Available   Stock
-------------------------------------------------------------
Comp1      3       1       NULL      67         100      33
Comp2    NULL      4       NULL      64         100      36
Comp48     3       5        2        131        100     -31

t1保持成品的处理t2包含某些产品所需的组件数量,加上相对运动。
我需要创建一个过程,用这种方式更新表t2中的request列:
t2.request=prod1*(t1.request其中prodid=prod1)+prod2*(t1.request其中prodid=prodt2)等。
由于不知道如何在sql中执行此操作,我在excel上的vba中执行了此操作('hmwcomp'完全更新t2),它可以工作,但操作员抱怨此操作需要几秒钟。因为行数不到50行,最多可以有3-4个同时连接,所以我想知道在mysql的服务器上执行这个操作是否会使操作更快。
如果是,我该怎么办?
vba代码:

Option Explicit
public SRow as integer
public Conn as ADODB.Connection
public RS1 as ADODB.Recordset

Public Sub HMWComp()
Call ConnectDB' An external procedure to connect to DB...'
SRow = 1
Dim rsqui As ADODB.Recordset
Set rsqui = New ADODB.Recordset
rsqui.Open "select CompID from T2;", Conn, adOpenDynamic
Dim Numero As Integer
rsqui.MoveFirst
Do
    Numero = HowManyComponents(rsqui!CompID)
    Conn.Execute "update T2 set request=" & Numero & " where CompID=" & rsqui!CompID & ";"
    rsqui.MoveNext
Loop Until rsqui.EOF
rsqui.Close
End Sub

Public Function HowManyComponents(ByVal Component As String) As Integer
Dim WSDest as Worksheet
Set WSdest = ThisWorkbook.Sheets("Temp")
WSdest.Cells.Clear
Set RS1 = New ADODB.Recordset
RS1.Open "select column_name from information_schema.columns where table_name='T2' and column_name<>'CompID' and column_name<>'Request' and column_name<>'Available' and column_name<>'Stock';", Conn, adOpenDynamic
RS1.MoveFirst
Do
    Set RSF = New ADODB.Recordset
    RSF.Open "select " & RS1(0) & " from T2 where CompID=" & Component & ";", Conn, adOpenStatic
    If IsNull(RSF(0)) = False Then
        With WSdest
            .Cells(SRow, 1) = RSF(0)
            RSF.Close
            Set RSF = New ADODB.Recordset
            RSF.Open "select Request from T1 where ProdiD='" & RS1(0) & "';", Conn, adOpenStatic
            If IsNull(RSF(0)) = False Then
                .Cells(SRow, 2) = RSF(0)
            Else
                .Cells(SRow, 2) = 0
            End If
            RSF.Close
            .Cells(SRow, 3) = .Cells(SRow, 1) * .Cells(SRow, 2)
        End With
        SRow = SRow + 1
    Else
        RSF.Close
    End If
    RS1.MoveNext
Loop Until RS1.EOF
RS1.Close
HowManyComponents = WorksheetFunction.Sum(WSdest.Columns(3))
Exit Function
End Function

我试图写一个过程,但不幸的是它只更新(准确地)最后一条记录。如何正确处理循环?
sql存储过程:

BEGIN
DECLARE done INT DEFAULT FALSE;
declare col_name varchar(20);
declare CodName varchar(30);
declare NR int;
declare CR int;
declare PERC int;
declare TOT int;

declare cur1 CURSOR FOR 
select column_name from information_schema.columns where table_name='T2' and column_name<>'CompID';

declare cur2 cursor for
select codice from T2;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

set NR=(select count(*) from T2);
open cur1;
set CR=0;
truncate table debug_table;
read_loop: loop
        fetch cur1 into col_name;
        if done then leave read_loop; end if;
        set TOT=0;
        set CR=CR+1;
        set @sqlstr=concat('select @QC := richieste from T1 where ProdID=',char(39),col_name,char(39),';');
        insert into debug_table(id,msg1) values(cr,@sqlstr);
        prepare stmt from @sqlstr;
        execute stmt ;
        deallocate prepare stmt;
        open cur2;
        write_loop: loop
            set PERC=0;
            if done then leave write_loop; end if;
            fetch cur2 into CodName;
            set @sqlstr=concat('select @QP := coalesce(',col_name,',0) from T2 where CompID=',char(39),CodName,char(39),';');
            update debug_table set msg2=@sqlstr where id=cr;
            prepare stmt from @sqlstr;
            execute stmt ;
            deallocate prepare stmt;
            set PERC=@QC*@QP;
            set TOT=TOT+PERC;
        end loop;
        close cur2;
        set @sqlstr=concat('update T2 set richiesti=',tot,' where CompID=',char(39),CodName,char(39),';');
        update debug_table set msg3=@sqlstr where id=cr;
        prepare stmt from @sqlstr;
        execute stmt ;
        deallocate prepare stmt;
end loop;
END
tp5buhyn

tp5buhyn1#

create table #t1 (ProdID   varchar(10), Request   int, Available   int,Stock int)
insert #t1 values
 ( 'Prod1'    ,  17 ,        50        ,33)
,( 'Prod2'    ,  16 ,        40        ,24)
,( 'Prod43'   ,   0 ,        10        ,10)

create table #t2 (CompID   varchar(10), Prod1   int, Prod2   int, Prod43   int, Request   int, Available   int, Stock int)
insert #t2 values  
  ('Comp1'   ,   3 ,      1       ,NULL,      00    ,     100     , 33) -- 00 for request. After the sp, these values are will be updated.
, ('Comp2'  ,  NULL,      4       ,NULL,      00    ,     100     , 36) -- 00 for request. After the sp, these values are will be updated.
, ('Comp48',     3 ,      5       , 2  ,      00   ,     100     ,-31)  -- 00 for request. After the sp, these values are will be updated.

create procedure updateRequest as
begin

 select CompID, sum(value*t1.Request) val into #update from (
  select *
  from #t2
  unpivot (value for Prods in ([prod1],[prod2],[prod43])) up
 ) t2
 join #t1 t1
 on t2.Prods = t1.ProdID
 group by CompID

 update t
 set t.Request = u.val
 from #t2 t
 join #update u
 on t.CompID = u.CompID

 select * from #t2
 drop table #update
end

select * from #t2
exec updateRequest

如果查询需要更新,请回复我。

gzszwxb4

gzszwxb42#

您应该尝试将子查询与 INFORMATION_SCHEMA 表,您可以在其中对 T2 表格:

(SELECT COLUMN_NAME AS ProdID
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA='yourdatabasename' 
        AND TABLE_NAME='T2'
        AND TABLE_NAME LIKE 'Prod%') TAUX

然而,由于错误的db模式规范化,这仍然是一项困难的工作。

相关问题