子查询删除语句不起作用

时间:2014-08-27 04:01:00

标签: mysql sql

我不知道这句话有什么问题,但每当我运行这个时我总是会收到错误

这是我的sql:

DELETE FROM tbl_usersinfo
WHERE users_lname IN
(SELECT users_lname FROM tbl_usersinfo WHERE users_lname = 'asd')

这是我的错误:

#1093 - 您无法在FROM子句中为更新指定目标表'tbl_usersinfo'

3 个答案:

答案 0 :(得分:0)

注意,

(SELECT users_lname FROM tbl_usersinfo WHERE users_lname = 'asd')

等于

users_lname='asd'

所以,sql可能是

DELETE FROM tbl_usersinfo WHERE users_lname = 'asd'

答案 1 :(得分:0)

您无法指定要删除的目标表。

首先创建临时表,然后在查询中同时使用临时表

CREATE TABLE IF NOT EXISTS table2 AS (SELECT * FROM tbl_usersinfo)

DELETE FROM tbl_usersinfo
WHERE users_lname IN
(SELECT users_lname FROM table2 WHERE users_lname = 'asd')

Sample Demo here

答案 2 :(得分:0)

DELETE FROM tbl_usersinfo
WHERE users_lname IN
(select * from (SELECT users_lname FROM tbl_usersinfo WHERE users_lname = 'asd') as t)