根据表2中发现的重复记录,我正在尝试执行一个查询来删除表1中的记录。id是这两个表之间的公共链接。数据库是oracle。我是写查询的新手,下面是我提出的查询,到目前为止还没有解决。有没有人能提个建议?
实际上,我正在尝试删除基于表1中的id的记录,条件是表2中存在该id的重复记录以及多一个列?下面是我得到的错误信息,我真的不确定查询是否准确,或者是否需要重写整个查询本身?
“无效的sql语句”- ORA-00900
DELETE TABLE AS m WHERE m.id IN
(SELECT id from table2 t WHERE ROWID >
(SELECT MIN(ROWID) FROM table2 r WHERE t.column2 = r.column2);发布于 2019-01-05 06:00:33
您可以尝试执行以下操作(仅使用tab作为语句中的表):
create table tab ( id int, val int );
insert into tab values(1 ,331);
insert into tab values(1 ,332);
insert into tab values(2 ,333);
insert into tab values(2 ,333);
select * from tab;
ID VAL
1 331
1 332
2 333
2 333
delete tab a
where
rowid >
(select min(rowid)
from tab b
where b.id=a.id
group by b.id);
select * from tab;
ID VAL
1 331
2 333附注:table是Oracle中的保留关键字,不能是表名,也可以使用"table"。
发布于 2019-01-05 08:28:24
你会以某种方式找到复制品。这有点不清楚,但也许:
select t2.column2, count(*)
from table2 t2
group by t2.column2
having count(*) >= 2;然后,您可以将此代码放入delete
delete from m
where m.id in (select t2.column2
from table2 t2
group by t2.column2
having count(*) >= 2
);https://stackoverflow.com/questions/54046468
复制相似问题