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

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的问题的更多相关文章

  1. MYSQL 1093 之You can't specify target table for update in FROM clause解决办法

    You can't specify target table for update in FROM clause含义:不能在同一表中查询的数据作为同一表的更新数据. 出现以上错误,是因为想将表自身的字 ...

  2. mysql错误:1093-You can’t specify target table for update in FROM clause的解决方法

    update语句中包含的子查询的表和update的表为同一张表时,报错:1093-You can’t specify target table for update in FROM clause my ...

  3. MYSQL错误:You can't specify target table for update in FROM clause

    这句话意思是:不能先select再更新(修改)同一个表. 可以再外嵌套多一层,这个问题只有mysql有,mssql和oracle都没有. # 出错delete from Person where Id ...

  4. 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 ...

  5. 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中You c ...

  6. 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这个表( ...

  7. mysql出现You can’t specify target table for update in FROM clause

    在mysql执行下面语句时报错: You can’t specify target table for update in FROM clause UPDATE edu_grade_hgm_1 ' W ...

  8. 解决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这个表( ...

  9. 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这个表( ...

随机推荐

  1. 自己封装一个弹窗JS

    在我们平时的开发中,一定有很多地方需要用到弹窗提示功能.而系统自带的弹窗奇丑无比,而且我们不能自主控制.因此我们在开发过程中,自己封装一个弹窗JS方便我们使用. 代码demo如下: // JavaSc ...

  2. laravel whereNotIn where子查詢

    子查詢寫法 $stores = Stores::select('id','name')->whereNotIn('id', function ($query) use($id){ $query- ...

  3. Linux touch命令详解

    Linux touch命令 Linux touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间.若文件不存在,系统会建立一个新的文件. 用法: touch [-acfm][-d<日 ...

  4. SQL Agent 服务无法启动

    问题现象 从阿里云上镜像过来的一台的数据库服务器,SQL Agent服务启动不了,提示服务启动后停止. 如下是系统日志和SQL Agent的日志 SQLServerAgent could not be ...

  5. Bootstrap格式转换代码

    网址:http://www.w3cschool.cc/bootstrap/bootstrap-responsive-utilities.html <div class="contain ...

  6. gcc centos 新版本的安装方法

    因为centos默认安装的gcc是GCC 4.*.* 是不支持 C++11 的,所以有些新的程序或软件要安装就行要升级GCC,否则无法编译通过 一.如下步骤安装不成功(yum install devt ...

  7. 减肥标准BMI指数

    原文: https://baike.baidu.com/item/BMI%E6%8C%87%E6%95%B0/4477882?fromtitle=%E4%BD%93%E9%87%8D%E6%8C%87 ...

  8. C#操作wps、excel

    比如打开表格,如下 object openEt() { ]; Type wpsAppName; string progID = "KET.Application";// " ...

  9. java使用jdbc连接oracle(其他数据库类似)

    最基本的Oracle数据库连接代码: 1.右键项目->构建路径->配置构建路径,选择第三项“库”,然后点击“添加外部Jar”,选择“D:\Oracle\app\oracle\product ...

  10. rabbitmq (四) 路由

    上文讲的是广播类型fanout 本章讲 direct和topic. 当使用广播类型fanout的时候: routingKey字段不起作用. direct:精确匹配 routingKey:匹配一个单词, ...