首页 技术 正文
技术 2022年11月14日
0 收藏 423 点赞 2,598 浏览 1793 个字

删除重复行

 DELETE FROM ecm_member_login_session
WHERE (number , client_code) IN (
SELECT number, client_code FROM mall.ecm_member_login_session GROUP BY number , client_code HAVING COUNT(*) > 1)
AND update_time NOT IN (
SELECT MAX(update_time) FROM mall.ecm_member_login_session GROUP BY number , client_code HAVING COUNT(*) > 1);

但是报1093错误

 14:57:04    DELETE FROM ecm_member_login_session  WHERE (number , client_code) IN (   SELECT number, client_code FROM mall.ecm_member_login_session GROUP BY number , client_code HAVING COUNT(*) > 1)   AND update_time NOT IN (   SELECT MAX(update_time) FROM mall.ecm_member_login_session GROUP BY number , client_code HAVING COUNT(*) > 1)     Error Code: 1093. You can't specify target table 'ecm_member_login_session' for update in FROM clause    0.046 sec

后来在 嵌套查询语句里面再嵌套一层即可, 是因为mysql限制update(包括delete)操作表名与嵌套子查询语句里的表名一样的原因。

语句改为如下

 DELETE FROM ecm_member_login_session
WHERE (number , client_code) IN (
SELECT number, client_code from (SELECT number, client_code FROM ecm_member_login_session GROUP BY number , client_code HAVING COUNT(*) > 1) A)
AND update_time NOT IN (
SELECT update_time from (SELECT MAX(update_time) FROM ecm_member_login_session GROUP BY number , client_code HAVING COUNT(*) > 1) A);

mysql文档:

 上面是目前MYSQL5.0仍然有的限制,文档中说: In general, you cannot modify a table and select from the same table in a subquery. For example, this limitation applies to statements of the following forms: DELETE FROM t WHERE ... (SELECT ... FROM t ...);UPDATE t ... WHERE col = (SELECT ... FROM t ...);{INSERT|REPLACE} INTO t (SELECT ... FROM t ...); Exception: The preceding prohibition does not apply if you are using a subquery for the modified table in the FROM clause. Example: UPDATE t ... WHERE col = (SELECT (SELECT ... FROM t...) AS _t ...); Here the prohibition does not apply because a subquery in the FROM clause is materialized as a temporary table, so the relevant rows in t have already been selected by the time the update to t takes place. 

参考:

mysql error 1093 解决方法

SQL删除重复数据只保留一条

http://blog.163.com/xiaoqiu_1120/blog/static/121632322007112411424982/

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,501
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,915
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,748
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,505
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,143
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,306