簡單:因爲不允許使用子查詢中要刪除的表。只是separatly運行SELECT
查詢,並將結果進行刪除...
RTM,它明確規定,這是這裏不允許:
Incorrectly used table in subquery:
Error 1093 (ER_UPDATE_TABLE_USED)
SQLSTATE = HY000
Message = "You can't specify target table 'x'
for update in FROM clause"
This error occurs in cases such as the following, which attempts to modify a table and select from the same table in the subquery:
UPDATE t1 SET column2 = (SELECT MAX(column1) FROM t1);
You can use a subquery for assignment within an UPDATE statement because subqueries are legal in UPDATE and DELETE statements as well as in SELECT statements. However, you cannot use the same table (in this case, table t1) for both the subquery FROM clause and the update target.
特別是最後一句是很重要的:然而,對於子查詢FROM子句和更新目標,您都不能使用同一個表(在本例中爲表t1)。
周圍有這樣的哈克的方式,這是愚蠢的,但它的工作我最後一次檢查,這是包裝你的子查詢的另一個子(WHERE x IN (SELECT id FROM (SELECT id FROM tbl...))
)。但這種方式在各方面都很糟糕
你能不能簡單地做'刪除訪問者WHERE ip_address ='「。$ ipAddress。''AND user_id =''ORDER BY user_id DESC LIMIT 1' –
@ Fred-ii-我認爲你的意思是'AND user_id <>''' – Digzol
@Digzol我會說我站*糾正* ;-) –