Sql选择加入和删除在一起

时间:2014-11-16 09:44:53

标签: mysql sql mysqli

表大师

|  id  |   user  |
|  1   |  user1  |
|  3   |  user2  |
|  4   |  user3  |

table ver

| id_v |  value  |
|  2   |  value1 |
|  3   |  value2 |

我必须删除表ver

上的id_v 2

我'有这个查询

DELETE FROM `ver` WHERE id_v in (SELECT v.id_v
FROM master m
RIGHT JOIN ver v
ON m.id = v.id_v
WHERE m.id IS NULL)

但这是错误

You can't specify target table 'ver' for update in FROM clause

3 个答案:

答案 0 :(得分:1)

试试这个

DELETE FROM `ver` WHERE id_v in ( SELECT id_v from
(SELECT v.id_v FROM master m
RIGHT JOIN ver v
ON m.id = v.id_v
WHERE m.id IS NULL) as temp )

答案 1 :(得分:1)

可以使用以下

完成
delete 
v
from ver v
left join master m on m.id = v.id_v
where m.id is null

<强> Demo

答案 2 :(得分:0)

您不能使用table ver的子查询从表ver本身中删除某些内容。

你是说这个?

delete from ver where id_v is null;