删除冗余数据

WHW1984 2009-11-02 03:42:46
在mysql中一个表如下
create table testtable(
id int(4) not null primary key,
name varchar(12),
value varchar(12)
)

插入数据如下:
insert into testtable values(1, 'ww', 'ee');
insert into testtable values(2, 'ww', 'ee');
insert into testtable values(4, 'ww', 'ee');
insert into testtable values(5, 'ww', 'ee');
insert into testtable values(7, 'ww', 'ee');
insert into testtable values(8, 'qq', 'aa');
insert into testtable values(9, 'qq', 'aa');
insert into testtable values(10, 'qq', 'aa');
insert into testtable values(11, 'qq', 'aa');
insert into testtable values(12, 'rr', 'yy');
insert into testtable values(13, 'rr', 'yy');
insert into testtable values(14, 'rr', 'yy');
insert into testtable values(15, 'rr', 'yy');

删除其中的冗余数据?
...全文
163 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
WWWWA 2009-11-02
  • 打赏
  • 举报
回复
测试了一下,
DELETE a FROM njzg1 a INNER JOIN njzg1 b ON name=a.name and a.id>b.id and a.value=b.value;
WHW1984 2009-11-02
  • 打赏
  • 举报
回复
zhoupuye正解
至少我执行了只有那个可以
storyxsj 2009-11-02
  • 打赏
  • 举报
回复
试试吧
select t1.* from testtable t1,( select id from testtable t group by name
,value) t2 where t1.id = t2.id;
huangdh12 2009-11-02
  • 打赏
  • 举报
回复
delete table a where a.id>(select min(id) from table b where a.name=b.name and a.value=b.value);

这个应该是可以
阿_布 2009-11-02
  • 打赏
  • 举报
回复

delete t1 from testtable t1,testtable t2
where t1.id>t2.id and t1.name=t2.name and t1.value=t2.value;
WHW1984 2009-11-02
  • 打赏
  • 举报
回复
是InnoDB
WHW1984 2009-11-02
  • 打赏
  • 举报
回复
这个我也试过 没问题的
但是我要的是删除啊 大哥
berryluo 2009-11-02
  • 打赏
  • 举报
回复
LZ的mysql表类型是InnoDB还是MyISAM呢?建议看看这两种类型的差别。。。。。
WWWWA 2009-11-02
  • 打赏
  • 举报
回复
select * from from tt a where exists(select 1 from tt where name=a.name and a.id>b.id)

这样试试,我在SQLYOG 8.12+mysql5.32 测试没有问题
WWWWA 2009-11-02
  • 打赏
  • 举报
回复
不会吧,在MYSQL5.1下没有问题
你的SQL语句
WHW1984 2009-11-02
  • 打赏
  • 举报
回复
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where exists(select 1 from testtable where name=a.name and a.id>b.id)' at line 1
(0 ms taken)
是我的SQLyog的问题还是mysql版本的问题 还是你的sql语句的问题?
WWWWA 2009-11-02
  • 打赏
  • 举报
回复
delete from tt a where exists(select 1
from tt where name=a.name and a.id>b.id)
WHW1984 2009-11-02
  • 打赏
  • 举报
回复
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where not exists(select 1 from testtable where name=a.name and a.id>b.id)' at line 1
(0 ms taken)
WWWWA 2009-11-02
  • 打赏
  • 举报
回复
delete from tt a where not exists(select 1
from tt where name=a.name and a.id>b.id)
WHW1984 2009-11-02
  • 打赏
  • 举报
回复
对啊 把整条记录都删除,保留id最小的那个数据
WWWWA 2009-11-02
  • 打赏
  • 举报
回复
冗余标准?
name、value重复?

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧