mysql修改删除You can't specify target table for update in FROM clause的问题

表中出现重复数据,需要删除重复数据,只保留一条

DELETE
FROM
    crm_participant
WHERE
    id IN (
SELECT
    c.id cid
FROM
    crm_participant c
WHERE
    c.parentPhone IN ( SELECT a.parentPhone FROM crm_participant a GROUP BY a.parentPhone HAVING count( a.parentPhone ) > 1 )
    AND c.id NOT IN ( SELECT min( b.id ) FROM crm_participant b GROUP BY b.parentPhone HAVING count( b.parentPhone ) > 1 )
ORDER BY
    c.parentPhone 
    )

错误信息:

> 1093 - You can‘t specify target table ‘crm_participant‘ for update in FROM clause
> 时间: 0.005s

  

问题分细:
       mysql不允许对同一个表中查出来的数据作为条件,在执行跟新操作。 在一条 sql 语句中不能先查出来部分内容,再同时又对当前表作修改。
       
解决办法:这个是正确的sql,其实就是对上边的红色部分的查询sql进行了一层包裹。让查询出来的信息被一个  select 包裹一下,然后作为条件就可以了

DELETE
FROM
    crm_participant
WHERE
    id IN (
SELECT
    v.cid
FROM
    (
SELECT
    c.id cid
FROM
    crm_participant c
WHERE
    c.parentPhone IN ( SELECT a.parentPhone FROM crm_participant a GROUP BY a.parentPhone HAVING count( a.parentPhone ) > 1 )
    AND c.id NOT IN ( SELECT min( b.id ) FROM crm_participant b GROUP BY b.parentPhone HAVING count( b.parentPhone ) > 1 )
ORDER BY
    c.parentPhone
    ) v
     )

mysql修改删除You can't specify target table for update in FROM clause的问题

原文地址:https://www.cnblogs.com/renjianjun/p/10412703.html

时间: 2024-08-07 02:16:51

mysql修改删除You can't specify target table for update in FROM clause的问题的相关文章

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

使用mysql在删除表中重复记录 delete from user where username in (select user name form(select username from user group by username having count(username)>1)); 遇到mysql报错You can't specify target table for update in FROM clause 上网百度了下原来是mysql中不允许先select出同一表中的某些值,再u

mysql中You can’t specify target table for update in FROM clause错误解决方法

mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中). 例如下面这个sql: 1 delete from tbl where id in 2 ( 3 select max(id) from tbl a where EXISTS 4 ( 5 select 1 from tbl b where a.tac=b.tac group by ta

MySQL中You can't specify target table for update in FROM clause一场

mysql中You can't specify target table <tbl> for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中). 如下l: 需要将select出的结果再通过中间表select一遍,就可以规避了错误. 如下: PS:这个问题只出现于mysql,sql service 和 oracle 不会出现此问题. MySQL中You can't specify target table for

mysql中You can&#39;t specify target table for update in FROM clause错误

MySQL中You can't specify target table <tbl> for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中). 例如下面这个sql: delete from tbl where id in ( select max(id) from tbl a where EXISTS ( select 1 from tbl b where a.tac=b.tac group by tac H

mysql error:You can&#39;t specify target table for update in FROM clause

mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中). 例如下面这个sql: 代码如下: delete from tbl where id in ( select max(id) from tbl a where EXISTS ( select 1 from tbl b where a.tac=b.tac group by tac HA

mysql uodate 报错 You can&#39;t specify target table &#39;**&#39; for update in FROM clause

You can't specify target table 'sc' for update in FROM clause 背景:把“sc”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩: 上面的sql是我写的,执行就报这个错,这个原因说的是 不能从自己表里查数据再更新自己 解决方法:嵌套一层中间表 update sc set sc.score = (select t1.score from (select avg(sc1.score) score from sc sc1 where sc

MySQL can’t specify target table for update in FROM clause

翻译:MySQL不能指定更新的目标表在FROM子句 源SQL语句: [sql] view plain copy print? delete from t_official_sys_user where USER_NAME IN(SELECT USER_NAME FROM t_official_sys_user b group by b.`USER_NAME` having  count(1) > 1) 执行报以下错误: [sql] view plain copy print? [SQL] del

MySQL - 1093异常 - You can&#39;t specify target table &#39;t&#39; for update in FROM clause

有一个表示地区的表,表结构与数据大概如下表. ID NAME PARENT_ID 1 中国 2 广东省 1 3 广州市 2 4 荔湾区 3 5 越秀区 3 6 番禺区 3 7 小谷围街道 6 现为了查询方便,需要加一列PARENT_NAME,用以表示上级地区的名称(虽然不符合第三范式,传递依赖,但有时为了业务上的可行性.便利性,可以按实际情况考虑) ID NAME PARENT_ID PARENT_NAME 1 中国 2 广东省 1 3 广州市 2 4 荔湾区 3 5 越秀区 3 6 番禺区 3

MySQL问题:You can&#39;t specify target table &#39;表名&#39; for update in FROM clause

在MySQL中,写SQL语句的时候 ,可能会遇到You can't specify target table '表名' for update in FROM clause这样的错误,它的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中). 1.问题是如何出现的? 数据准备 CREATE TABLE T_Person( pId INT PRIMARY KEY AUTO_INCREMENT, pName VARCHAR(20) ); INSERT INTO T_Pe