我在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')
有没有更好的方法来实现我期望的结果?
只是fyi,tbl_3最多有5行,而tbl_2最多可以包含3行同样的pid。因此,我希望在每个pid的单个表中显示5行内的三个表中的所有字段。我该怎么办?
答案 0 :(得分:1)
只需用以下SQL替换所有代码
(error "line 8 column 24: Sort mismatch at argument #1 for function (declare-fun <= (Int Int) Bool) supplied sort is (_ BitVec 32)")
(error "line 9 column 24: Sort mismatch at argument #1 for function (declare-fun <= (Int Int) Bool) supplied sort is (_ BitVec 32)")
(error "line 10 column 24: Sort mismatch at argument #1 for function (declare-fun <= (Int Int) Bool) supplied sort is (_ BitVec 32)")
(error "line 13 column 23: Sort mismatch at argument #1 for function (declare-fun > (Int Int) Bool) supplied sort is (_ BitVec 32)")
(error "line 16 column 24: Sort mismatch at argument #1 for function (declare-fun > (Int Int) Bool) supplied sort is (_ BitVec 32)")
(error "line 19 column 24: Sort mismatch at argument #1 for function (declare-fun <= (Int Int) Bool) supplied sort is (_ BitVec 32)")
(error "line 22 column 15: Sort mismatch at argument #1 for function (declare-fun <= (Int Int) Bool) supplied sort is (_ BitVec 32)")
(error "line 27 column 44: Sort mismatch at argument #1 for function (declare-fun > (Int Int) Bool) supplied sort is (_ BitVec 32)")
(error "line 29 column 52: Sort mismatch at argument #1 for function (declare-fun < (Int Int) Bool) supplied sort is (_ BitVec 32)")
(error "line 30 column 52: Sort mismatch at argument #1 for function (declare-fun < (Int Int) Bool) supplied sort is (_ BitVec 32)")
(error "line 33 column 23: Sort mismatch at argument #1 for function (declare-fun < (Int Int) Bool) supplied sort is (_ BitVec 32)")
sat
((A #x00000000))
((B #x00000000))
((C #x00000000))
((D #x00000000))
((F #x00000000))
根据聊天中指定的要求编辑以提供以下新方法:
&#34;我要找的是:每个pid的5条记录 tbl_sourcef,应该包含table1(行可以是多余的 单个pid),表2和表3(来自tbl_2和tbl_3的行不应该&t; 重复)在tbl_sourcef表&#34;
中
select *
from tbl_1 t1 left join
(select ISNULL(t2.pid, t3.pid) as merge_pid, t2.sid, t2.nrt2, t3.wid, t3.nrt3
from tbl_2 t2 full join tbl_3 t3 on t2.soid = t3.woid) as mrg_table
on t1.pid = mrg_table.merge_pid