删除子查询和LIKE

时间:2012-03-14 19:25:16

标签: mysql

如何使用像这样的查询删除行

DELETE FROM tbl
WHERE title LIKE CONCAT((select title from tbl where id = 10),'%') 

此查询返回错误

  

[Err] 1093 - 您无法为更新指定目标表'条目'   FROM子句

4 个答案:

答案 0 :(得分:3)

DELETE t
FROM 
    tableX AS t
  JOIN
    ( SELECT CONCAT(title, '%') AS titleL
      FROM tableX
      WHERE id = 10
    ) AS ti
    ON t.title LIKE ti.titleL

答案 1 :(得分:2)

解决该错误的一种方法是将其分解为两个单独的语句。

您可以选择要匹配的标题到用户定义的变量中,然后执行删除。

像这样:

SELECT title
INTO @title
FROM tbl 
WHERE id = 10;

DELETE FROM tbl
WHERE title LIKE CONCAT(@title,'%'); 

答案 2 :(得分:1)

我认为你只需要从行插入另一个来执行选择查询:

DELETE t1
FROM tbl as t1
WHERE t1.title LIKE CONCAT((select title from tbl as t2 where t2.id = 10),'%') 

答案 3 :(得分:1)

您需要以下内容:

DELETE tbl
  FROM tbl T1
       INNER JOIN (SELECT title FROM tbl WHERE id = 10) T2                   
    ON T1.title LIKE CONCAT(T2.title, '%')