您不能在FROM子句中为更新指定目标表'b'

时间:2012-06-13 04:37:07

标签: mysql

我需要知道在这个sql语句中我哪里做错了。我尝试在以前的线程中找到类似问题的解决方案,但没有一个可以解决我的问题。所以我想也许我的说法实际上是错误的。

UPDATE table1 b

LEFT JOIN table2 m ON b.ICNO=m.ICNO

SET b.SalMoveMth = '01'

WHERE
    m.Status!='6' AND 
    (DATE_FORMAT(startDateSand,'%m')='10' OR DATE_FORMAT(startDateSand,'%m')='11' OR 
        DATE_FORMAT(startDateSand,'%m')='12') AND 
    ((SELECT SalMoveMth FROM table1 WHERE ICNO=table2.ICNO ORDER BY SalMoveMthStDt DESC LIMIT 1)!='10').

谢谢。

2 个答案:

答案 0 :(得分:0)

where子句的最后一部分:

而不是:

where ICNO=table2.ICNO order by SalMoveMthStDt desc limit 1)!='10')

尝试:

where ICNO=table2.ICNO order by SalMoveMthStDt desc limit 1)<>'10')

还:

and b.SalMoveMth in (
((select SalMoveMth from table1 where ICNO=table2.ICNO order by 
SalMoveMthStDt desc limit 1)<>'10'))

答案 1 :(得分:0)

update table1 
set SalMoveMth = '01' where icno in 
  (select b.ICNO from table1 b 
   left join table2 m on b.ICNO=m.ICNO
   where m.Status!='6' 
   and (DATE_FORMAT(startDateSand,'%m')='10' or 
        DATE_FORMAT(startDateSand,'%m')='11' or 
        DATE_FORMAT(startDateSand,'%m')='12') 
   and b.SalMoveMth in (
     ((select SalMoveMth 
       from table1 
       where ICNO=table2.ICNO 
       order by SalMoveMthStDt desc limit 1)<>'10')
  )