在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
2条答案
按热度按时间tp5buhyn1#
如果查询需要更新,请回复我。
gzszwxb42#
您应该尝试将子查询与
INFORMATION_SCHEMA
表,您可以在其中对T2
表格:然而,由于错误的db模式规范化,这仍然是一项困难的工作。