【发布时间】:2015-12-11 18:53:41
【问题描述】:
我在mysql数据库中有下表
create table tbl_1(pid int, loc varchar(100), avaId int,xpId int,qf varchar(100));
create table tbl_2(soid int,pid int,sid int,name2 varchar(100), nrt2 int);
create table tbl_3(woid int,pid int,wid int,name3 varchar(100), nrt3 int);
create table tbl_sourcef(id int primary key auto_increment,pid int, loc varchar(100), avaId int,xpId int,qf varchar(100),sid int,nrt2 int,wid int,nrt3 int);
将数据插入上述表格后
insert into tbl_1 values (1000,'Bangalore',30,9,'ABC');
insert into tbl_2 values(0,1000,1,'name1',8);
insert into tbl_2 values(1,1000,8,'name2',5);
insert into tbl_2 values(2,1000,7,'name3',6);
insert into tbl_3 values(0,1000,2,'D1',9);
insert into tbl_3 values(1,1000,1,'D2',2);
insert into tbl_3 values(2,1000,3,'D3',0);
insert into tbl_3 values(3,1000,4,'D4',5);
下面是上面 tbl_1,tbl_2,tbl_3 中的行
我正在尝试像这样将这三个表合并到一个表中 -
这对一组 pid 正常工作。当我使用另一组 pid 时,它会失败,子查询返回超过 1 行。我找不到导致问题的原因
我正在使用一个名为 fupdate() 的存储过程
这是SP的定义:
CREATE PROCEDURE fupdate(
pid int,loc varchar(100),avaId int,xpId int,qf varchar(100)
)
begin
declare pi int Default 1;
WHILE pi <= 10 DO
insert into tbl_sourcef(pid,loc,avaId,xpId,qf)values(pid,loc,avaId,xpId,qf);
SET pi = pi + 1;
END WHILE;
begin
declare i int Default 1 ;
declare si int default 0;
declare es int;
set es=(select count(sid) from tbl_2 where pid=pid);
WHILE i <= es DO
update tbl_sourcef ff
set ff.sid=(select sid from tbl_2 where soid=si and pid=pid),
ff.nrt2=(select nrt2 from tbl_2 where soid=si and pid=pid)
where id=i and pid=pid;
SET i = i + 1;
SET si=si+1;
END WHILE;
end;
begin
declare wi int Default 1 ;
declare wii int default 0;
declare ew int;
set ew=(select count(wid) from tbl_3 where pid=pid);
WHILE wi <= ew DO
update tbl_sourcef ff
set ff.wid=(select wid from tbl_3 where woid=wii and pid=pid ),
ff.nrt3=(select nrt3 from tbl_3 where woid=wii and pid=pid)
where id=wi and pid=pid ;
SET wi = wi + 1;
SET wii=wii+1;
END WHILE;
end;
end
这就是我如何称呼我的 SP -
call fupdate(1000,'Bangalore',30,9,'ABC')
有没有更好的方法来达到我期望的结果?
仅供参考,tbl_3 最多有 5 行,tbl_2 最多可以包含 3 行相同的 pid。因此,我想为每个 pid 显示单个表中 5 行内的三个表中的所有字段。我该怎么做?
【问题讨论】:
-
您需要从 3 个表中选择记录?为什么您尝试使用 JOIN 创建过程而不是简单的 SELECT?
-
因为我有相同的 id(pid),我不能简单地加入...如果我这样做,我会从表 2 和表 3 中获得冗余行
-
您永远不应该逐行更新。这只是一种糟糕的编码习惯。这需要基于集合以及使用连接。如果你得到多个具有不同值的行,你需要有业务规则来确定哪个值是正确的。
-
非常感谢,我从连接开始,意识到我无法满足这个要求。还有其他方法可以达到预期的结果吗?