2012-07-05 89 views
0

我在下面查詢...查詢中有什麼問題

有沒有什麼問題呢?

delete from user_role 
WHERE user_id in (
    select u.user_id from user u, user_role ur 
    where u.USER_ID=ur.USER_ID and ur.ROLE_ID=4 and u.USER_ID not in (
     select user_id from referrers)); 
+3

我認爲你不能在'DELETE FROM user_role'裏面使用'SELECT user_role' ... – Marco

+0

你得到了什麼錯誤?從快速看,它似乎應該執行,但如果你有大量的數據,我認爲它會很慢。 – Braiba

+0

stackoverflow是不是一個測試datebase :) – xdazz

回答

5

正如在手冊中規定的DELETE Syntax

Currently, you cannot delete from a table and select from the same table in a subquery.

可以代替使用DELETEjoin在一起的表中的多個數據表的形式:

DELETE user_role 
FROM user_role 
    INNER JOIN user_id USING (USER_ID) 
    LEFT JOIN referrers USING (USER_ID) 
WHERE user_role.ROLE_ID = 4 AND referrers.USER_ID IS NULL 
+0

」這就是答案,很好的工作:) – Marco

+0

哇沒有任何表你怎麼能猜到這個東西eggyal .... Awsome ... – Ketan