【问题标题】:Combine rows from three tables into one for each set of pid (One reference id)为每组 pid 将三个表中的行合并为一个(一个参考 id)
【发布时间】:2026-01-14 17:30:02
【问题描述】:

我在mysql中有下表,

tbl_1:

 tbl_2:

 tbl_3:

从上面的三个表中,我试图将每组 pid 的行合并到一个表中,如下所示:

因此,tbl_3 将有最大行数 (4),tbl_2 将有最多 3 行,而 tbl_1 将有一个用于每个 pid 的行。

只有 tbl_1 行可以是冗余的,而不是 tbl_2 和 tbl_3。

我不能使用连接,因为我没有任何其他键可以引用,我应该在所有这三个表中只使用 pid 并将它们转储到单个表中。 如果我使用,我也会得到 tbl_2 和 tbl_3 的冗余数据。

出于这个原因,我使用了一个复杂的 SP,它为每组 pid 创建一个临时表,并为 tbl_2 和 tbl_3 更新它。 但有人告诉我,逐行更新而不是基于集合更新不是正确的做法。

如何使用 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);

我正在使用一个名为 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

请注意,当我使用第二组 pid 时,此 SP 失败 :(.. 不确定我是否有一些简单的方法可以实现预期的结果,而不是使用这个 SP。

这就是我如何称呼我的 SP -

call fupdate(1000,'Bangalore',30,9,'ABC')

【问题讨论】:

  • 发布你迄今为止尝试过的代码,这样会容易得多
  • 当然 Mohan,.. 让我添加 SP
  • 添加代码,请检查
  • 你对这三个表的期望输出是什么......过程太混乱了
  • 我想将前三个表合并到一个表中(新的临时表也可以)。它应该有我在屏幕截图4中显示的数据。tbl_3 行总是有最大值,tbl_2行数将小于 tbl_3 .. 这两个表中的行不应在最终表中重复。并且 tbl_1 行可以重复。请参考我添加的第四张截图

标签: mysql sql


【解决方案1】:

我建议您使用以下查询来获取屏幕截图 4 中的结果集,而无需使用您的存储过程:

WITH cte AS
(
   SELECT COALESCE(tt.pid, ttt.pid) AS pid, tt.sid, tt.name2, 
    ttt.wid, ttt.name3,ttt.nrt3
  FROM tbl_2 tt
  FULL OUTER JOIN tbl_3 ttt
    ON tt.pid = ttt.pid
    AND  tt.soid = ttt.woid 
)
select *
from tbl_1 t
JOIN cte c
  ON t.pid = c.pid;

【讨论】:

    最近更新 更多