2017-08-31 40 views
0

多值错误,我跑下面的查询,并得到了错误,您不能指定目标表更新在FROM子句MySQL的子查询返回更新询问

update xyz x 
set x.PID = (select c.PPID 
      from 
      table1 c where c.FFID 
      in (select b.FID 
       from table1 a 
       join xyz b on a.FFID = b.fID 
       and a.FFIRD=b.PID and b.flag='4' 
       group by b.FID 
       having count(1) =1) 
       ),flag='N' 
where x.FID = (select b.FID 
       from table1 a join xyz b on 
       a.FFID = b.FID and 
       a.FFIRD=b.PID and b.flag='4' 
       group by b.FID 
       having count(1) =1 
      ) 

我提到计算器和改变了我的代码如下图所示。但现在我得到的Subquery返回多于1行。请帮助

update xyz x 
set x.PID = (select abc.PPID 
      from (select c.PPID 
        from 
        table1 c 
        where c.FFID 
        in (select b.FID 
         from 
         table1 a join xyz b on a.FFID = b.fID 
         and a.FFIRD=b.PID and b.flag='4' 
         group by b.FID 
         having count(1) =1 
        ) as abc 
        ),flag='N' 
where x.FID = (select xyz.FID 
       from (select b.FID 
        from table1 a join xyz b on 
        a.FFID = b.FID and 
        a.FFIRD=b.PID and b.flag='4' 
        group by b.FID 
        having count(1) =1)as xyz 
       ) 
+0

什么是你想在这里做? –

+0

在子查询上它只要求一行返回。要么限制1,要么改变方法。 –

回答

0

根据您的工作脚本,你可以创建一个CTE,倒出数据成,然后从更新或沿着这些线路的变化:

WITH YourCTE (PPID) 
AS 
(select c.PPID 
     from 
     table1 c where c.FFID 
     in (select b.FID 
      from table1 a 
      join xyz b on a.FFID = b.fID 
      and a.FFIRD=b.PID and b.flag='4' 
      group by b.FID 
      having count(1) =1) 
      ),flag='N' 
where x.FID = (select b.FID 
      from table1 a join xyz b on 
      a.FFID = b.FID and 
      a.FFIRD=b.PID and b.flag='4' 
      group by b.FID 
      having count(1) =1 
     ) 
) 
UPDATE xyz x 
SET x.PID = (SELECT PPID FROM YourCTE)