1

我正在尝试UPDATE在 double 上执行操作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

你能发现我的错误吗?

4

3 回答 3

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
于 2013-08-28T19:30:34.357 回答
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
于 2013-08-28T19:52:14.777 回答
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
于 2013-08-28T19:34:54.603 回答