开发者

problem with DELETE FROM [duplicate]

开发者 https://www.devze.com 2023-03-27 20:17 出处:网络
This question already has an answer here: MySQL - How to delete from table when nested select uses that table?
This question already has an answer here: MySQL - How to delete from table when nested select uses that table? (1 answer) Closed 8 years ago.

I try to delete some datas, and this is the model of my Tables :

 --------------------
|       TABLE1       |
 --------------------
| idnode |idattribute|
|____________________|
|6414224 | 109       |
|6912048 | 74        |
|5632108 | 109       |
|5097234 | 109       |
|9874625 | 9         |


 --------------------
|       TABLE2       |
 --------------------
| idnode | value     |
|____________________|
|6414224 | BLABLA    |
|6414224 | BLA       |
|6414224 | BL        |
|5097234 | 14524     |
|5097234 | hihi      |

I need to delete into the table 2, all 'idnode' wich have 'idattribute' = 109 in table 1.

So, this my query, but it doesn't work :

DELETE FROM tab开发者_如何学Gole2 WHERE idnode IN 
(SELECT TAB1.idnode FROM TABLE2 as TAB2, TABLE1 as TAB1 where TAB2.idnode = TAB1.idnode and TAB1.idattribute = 109)

I obtain this error : You can't specify target table 'TABLE2' for update in FROM clause

Have you an idea ?


DELETE FROM table2 WHERE idnode IN 
(SELECT idnode FROM TABLE1 WHERE idattribute = 109)

How about this?


DELETE table2
    FROM table1 JOIN table2 USING (idnode)
    WHERE table1.idattribute = 109

would be another alternative.

0

精彩评论

暂无评论...
验证码 换一张
取 消