UPDATE和双INNER JOIN踢出mysql错误

时间:2013-08-28 19:23:40

标签: mysql sql

我正在尝试对UPDATE进行INNER JOIN,并收到以下错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM pro_users AS u INNER JOIN cAlerts AS c ON c.user_id = u.user_id I' at line 3

这是我的mysql代码:

 UPDATE u 
  SET u.active_member = 0
  FROM pro_users AS u
  INNER JOIN cAlerts AS c
   ON c.user_id = u.user_id
  INNER JOIN srAlerts AS s  
   ON s.user_id = c.user_id
  WHERE c.status=0  
  AND s.active=0 
  AND u.active_member = 1

你能发现我的错误吗?

3 个答案:

答案 0 :(得分:1)

你可以试试这个:

UPDATE 
    pro_users AS u
SET
    u.active_member = 0
WHERE
    u.active_member = 1 
    AND
    (
        SELECT c.user_id
        FROM cAlerts AS c
        WHERE c.user_id = u.user_id AND c.status = 0
        LIMIT 1
    ) IS NOT NULL 
    AND
    (
        SELECT s.user_id
        FROM srAlerts AS s
        WHERE s.user_id = u.user_id AND s.active = 0
        LIMIT 1
    ) IS NOT NULL

答案 1 :(得分:1)

这将是ISO / ANSI SQL进行更新的方式:

update pro_users u set active_member = 0
where u.active_member = 1
  and exists ( select *
               from cAlerts c
               where c.user_id = u.user_id
                 and c.status  = 0
             )
  and exists ( select *
               from srAlerts sr
               where sr.user_id = u.user_id
                 and sr.active = 0
             )

据我所知,from语句中带有或不带连接的update子句是Microsoft / Sybase Sql Server异常。

编辑注意:在http://dev.mysql.com/doc/refman/5.0/en/update.html的mySql手册中进行了一些翻译,并且一些SQLFiddling说这将有效:

update pro_users u
join cAlerts  c  on c.user_id  = u.user_id and c.status  = 0
join srAlerts sr on sr.user_id = u.user_id and sr.active = 0
set active_member = 0
where u.active_member = 1

答案 2 :(得分:0)

我认为这样可行:

UPDATE pro_users AS u
INNER JOIN cAlerts AS c
   ON c.user_id = u.user_id AND c.status=0
INNER JOIN srAlerts AS s  
   ON s.user_id = c.user_id AND s.active=0
SET u.active_member = 0
WHERE u.active_member = 1