想只留下一条又如何写。。
17 个解决方案
#1
无解。只能先全部删除,然后再插入一条。
没有主键的表,在正式设计中根本不允许出现
没有主键的表,在正式设计中根本不允许出现
#2
日志表有可能吧。。
基本上都会有主键,想oracle中,这样的情况是可以写出sql的。。但是db2同样的方法是不行的,这也算是db2设计的一个小问题啊。。呵呵
#3
没主键,不行
#4
(Instance:DB2INST1, Database: TEST1):select * from t
ID NAME
----------- --------------------
3 luoyoumou
4 zhongchangtian
2 record(s) selected.
(Instance:DB2INST1, Database: TEST1):insert into t(id,name) values(3,'luoyoumou')
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):insert into t(id,name) values(3,'luoyoumou')
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):insert into t(id,name) values(3,'luoyoumou')
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):select * from t
ID NAME
----------- --------------------
3 luoyoumou
4 zhongchangtian
3 luoyoumou
3 luoyoumou
3 luoyoumou
5 record(s) selected.
(Instance:DB2INST1, Database: TEST1):select distinct * from t
ID NAME
----------- --------------------
3 luoyoumou
4 zhongchangtian
2 record(s) selected.
(Instance:DB2INST1, Database: TEST1):create table t_tmp like t
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):insert into t_tmp select distinct * from t
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):select * from t_tmp
ID NAME
----------- --------------------
3 luoyoumou
4 zhongchangtian
2 record(s) selected.
(Instance:DB2INST1, Database: TEST1):delete from t
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):insert into t select * from t_tmp
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):drop table t_tmp
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):select * from t
ID NAME
----------- --------------------
3 luoyoumou
4 zhongchangtian
2 record(s) selected.
(Instance:DB2INST1, Database: TEST1):
#5
-- 转个弯就行了嘛,就这点破事也要问,就不能自己独立思考一下?
-- (1) 先创建一张与原表t一样结构的表t_tmp
-- (2) 用“去重”(distinct)后的记录行插入t_tmp
-- (3) 删除原表记录行,并用t_tmp表的去重后的记录行插入原表t中即可!
-- (4) 删除辅助表t_tmp
-- 完成!
-- (1) 先创建一张与原表t一样结构的表t_tmp
-- (2) 用“去重”(distinct)后的记录行插入t_tmp
-- (3) 删除原表记录行,并用t_tmp表的去重后的记录行插入原表t中即可!
-- (4) 删除辅助表t_tmp
-- 完成!
#6
恩 思路很好 人才
#7
先select distinct到一个temp table,然后把原表数据删除,再把temp table里的数据copy过去
#8
直接删除也是可以的,
create table tb(
id varchar(20),
name varchar(20)
);
insert into tb values('1', '111') ;
insert into tb values('1', '111') ;
insert into tb values('1', '111') ;
insert into tb values('2', '222') ;
insert into tb values('2', '222') ;
insert into tb values('2', '222') ;
insert into tb values('3', '333') ;
insert into tb values('3', '333') ;
insert into tb values('3', '333') ;
----只删除其中一条
delete from ( select (row_number() over( ) ) as rowid , x.*
from tb x
) as A
where rowid in ( select max(rowid_1)
from ( select (row_number() over( ) ) as rowid_1 , x.*
from tb x
) as B
where A.id = B.id and
A.name = B.name
) ;
----只保留其中一条
delete from ( select (row_number() over( ) ) as rowid , x.*
from tb x
) as A
where rowid not in ( select max(rowid_1)
from ( select (row_number() over( ) ) as rowid_1 , x.*
from tb x
) as B
where A.id = B.id and
A.name = B.name
) ;
#9
非常支持8楼的写的SQL,不错的Idea
直得学习..顶
直得学习..顶
#10
试试这个看
delet from (select rownumber() over() as row, a.* from (select * from tablename) a) b where b.row <= 1
delet from (select rownumber() over() as row, a.* from (select * from tablename) a) b where b.row <= 1
#11
支持4楼和8楼,不同的思路不同的解决方法,学习了。
#12
4楼的方法更常用一些,不过8楼的思路很好。需要说的是,这种情况是应该避免的。即使是有人说的日志表,至少也可以通过timestamp来标识的。
#13
可以的,LS的两种方法都是可行的,不过效率上都不咋地。
#14
建一张一摸一样的表xxx_a insert ino xxx_a select distinct * from xxx;
#15
最简单的办法是增加一个自增列,然后删除自增号大于1的,另外想删那条都可以,完了在drop自增列。
#16
数据量大的话,用删除导入,数据量少的话,可以用sql的方法。
#17
平时都用10L的方法,原因就是有的表比较大,几亿十几亿,这样删除重复列或者说脏数据是比较快捷的。
如果不是所有列都一致,只是有一些列一致。比如ID有一致的,那么删除ID一致任意一行,就可以改一下,这么写:
delete from (select a.*, rownumber()over(partition by a.id) as row from test1 a) b where b.row<=1;
如果不是所有列都一致,只是有一些列一致。比如ID有一致的,那么删除ID一致任意一行,就可以改一下,这么写:
delete from (select a.*, rownumber()over(partition by a.id) as row from test1 a) b where b.row<=1;
#1
无解。只能先全部删除,然后再插入一条。
没有主键的表,在正式设计中根本不允许出现
没有主键的表,在正式设计中根本不允许出现
#2
日志表有可能吧。。
基本上都会有主键,想oracle中,这样的情况是可以写出sql的。。但是db2同样的方法是不行的,这也算是db2设计的一个小问题啊。。呵呵
#3
没主键,不行
#4
(Instance:DB2INST1, Database: TEST1):select * from t
ID NAME
----------- --------------------
3 luoyoumou
4 zhongchangtian
2 record(s) selected.
(Instance:DB2INST1, Database: TEST1):insert into t(id,name) values(3,'luoyoumou')
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):insert into t(id,name) values(3,'luoyoumou')
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):insert into t(id,name) values(3,'luoyoumou')
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):select * from t
ID NAME
----------- --------------------
3 luoyoumou
4 zhongchangtian
3 luoyoumou
3 luoyoumou
3 luoyoumou
5 record(s) selected.
(Instance:DB2INST1, Database: TEST1):select distinct * from t
ID NAME
----------- --------------------
3 luoyoumou
4 zhongchangtian
2 record(s) selected.
(Instance:DB2INST1, Database: TEST1):create table t_tmp like t
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):insert into t_tmp select distinct * from t
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):select * from t_tmp
ID NAME
----------- --------------------
3 luoyoumou
4 zhongchangtian
2 record(s) selected.
(Instance:DB2INST1, Database: TEST1):delete from t
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):insert into t select * from t_tmp
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):drop table t_tmp
DB20000I The SQL command completed successfully.
(Instance:DB2INST1, Database: TEST1):select * from t
ID NAME
----------- --------------------
3 luoyoumou
4 zhongchangtian
2 record(s) selected.
(Instance:DB2INST1, Database: TEST1):
#5
-- 转个弯就行了嘛,就这点破事也要问,就不能自己独立思考一下?
-- (1) 先创建一张与原表t一样结构的表t_tmp
-- (2) 用“去重”(distinct)后的记录行插入t_tmp
-- (3) 删除原表记录行,并用t_tmp表的去重后的记录行插入原表t中即可!
-- (4) 删除辅助表t_tmp
-- 完成!
-- (1) 先创建一张与原表t一样结构的表t_tmp
-- (2) 用“去重”(distinct)后的记录行插入t_tmp
-- (3) 删除原表记录行,并用t_tmp表的去重后的记录行插入原表t中即可!
-- (4) 删除辅助表t_tmp
-- 完成!
#6
恩 思路很好 人才
#7
先select distinct到一个temp table,然后把原表数据删除,再把temp table里的数据copy过去
#8
直接删除也是可以的,
create table tb(
id varchar(20),
name varchar(20)
);
insert into tb values('1', '111') ;
insert into tb values('1', '111') ;
insert into tb values('1', '111') ;
insert into tb values('2', '222') ;
insert into tb values('2', '222') ;
insert into tb values('2', '222') ;
insert into tb values('3', '333') ;
insert into tb values('3', '333') ;
insert into tb values('3', '333') ;
----只删除其中一条
delete from ( select (row_number() over( ) ) as rowid , x.*
from tb x
) as A
where rowid in ( select max(rowid_1)
from ( select (row_number() over( ) ) as rowid_1 , x.*
from tb x
) as B
where A.id = B.id and
A.name = B.name
) ;
----只保留其中一条
delete from ( select (row_number() over( ) ) as rowid , x.*
from tb x
) as A
where rowid not in ( select max(rowid_1)
from ( select (row_number() over( ) ) as rowid_1 , x.*
from tb x
) as B
where A.id = B.id and
A.name = B.name
) ;
#9
非常支持8楼的写的SQL,不错的Idea
直得学习..顶
直得学习..顶
#10
试试这个看
delet from (select rownumber() over() as row, a.* from (select * from tablename) a) b where b.row <= 1
delet from (select rownumber() over() as row, a.* from (select * from tablename) a) b where b.row <= 1
#11
支持4楼和8楼,不同的思路不同的解决方法,学习了。
#12
4楼的方法更常用一些,不过8楼的思路很好。需要说的是,这种情况是应该避免的。即使是有人说的日志表,至少也可以通过timestamp来标识的。
#13
可以的,LS的两种方法都是可行的,不过效率上都不咋地。
#14
建一张一摸一样的表xxx_a insert ino xxx_a select distinct * from xxx;
#15
最简单的办法是增加一个自增列,然后删除自增号大于1的,另外想删那条都可以,完了在drop自增列。
#16
数据量大的话,用删除导入,数据量少的话,可以用sql的方法。
#17
平时都用10L的方法,原因就是有的表比较大,几亿十几亿,这样删除重复列或者说脏数据是比较快捷的。
如果不是所有列都一致,只是有一些列一致。比如ID有一致的,那么删除ID一致任意一行,就可以改一下,这么写:
delete from (select a.*, rownumber()over(partition by a.id) as row from test1 a) b where b.row<=1;
如果不是所有列都一致,只是有一些列一致。比如ID有一致的,那么删除ID一致任意一行,就可以改一下,这么写:
delete from (select a.*, rownumber()over(partition by a.id) as row from test1 a) b where b.row<=1;