我有两个表tblA(Pid int,Lid int,coming_Lid int)
(123, 10,null),
(345, 10,null),
(567, 11, null),
(457, 11,null)
tblB(Pid int,Lid int,isActive int)
(123,10,1),
(123,11,0),
(123,12,0),
(123,13,1),
(345,10,1),
(345,11,1),
(567,11,1),
(567,12,1),
(457,11,1),
(457,12,0),
(457,13,1)
现在,我想从tblB填充tblA.upcoming_Lid,其中下一个盖子对同一个Pid是活动的。 来自tblA的期望结果:
(123, 10,13),
(345, 10,11),
(567, 11,12),
(457, 11,13)
谢谢!
答案 0 :(得分:3)
此SQL Fiddle演示了以下查询:
UPDATE tblA
SET upcoming_Lid =
(
SELECT Lid
FROM tblB
WHERE tblA.Pid = tblB.Pid
AND tblB.isActive = 1
AND tblB.Lid > tblA.Lid
)
答案 1 :(得分:0)
您可以使用聚合函数来填充该表。
这样的事情:
create table new_table as
select Pid, min(Lid), max(Lid)
where tblB.isActive = 1
from tblB group by Pid;