2013-08-28 90 views
1

我試圖在雙INNER JOIN做一個UPDATE,並得到以下錯誤:UPDATE和雙INNER JOIN踢了MySQL錯誤

#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 

你能發現我的錯誤嗎?

+0

使用UPDATE pro_users,我認爲它不知道的別名。 – Mihai

+0

當我使用該語法時,我不使用set子句中的別名。 –

+0

我嘗試使用「更新pro_users」,而不是「更新你」,並沒有奏效。 – themerlinproject

回答

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 
      ) 

據我所知,在update聲明中,有或沒有連接的from子句是Microsoft/Sybas e 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 
+0

ISO/ANSI SQL方式是讓我通過的唯一語法,可以這麼說。謝謝 – themerlinproject

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 
+0

+1單獨的select語句起作用 – themerlinproject

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