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

问题描述 投票:0回答:2

我在MySQL Community 5.7 DB上有两个表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
mysql
2个回答
0
投票

您应该尝试在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架构规范化,这仍然是一项艰巨的工作。


0
投票
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

如果查询需要更新,请将我还原。

© www.soinside.com 2019 - 2024. All rights reserved.