您好我删除行时遇到问题, 我想在数据库中保留两行并删除所有其他行 我在此链接中找到了一个查询for delete row 但它给出了mysql语法限制功能。 但在oracle中没有可用的限制变量。 我怎么能这样做?
DELETE FROM Table
WHERE DateColumn NOT IN (SELECT DateColumn
FROM Table
GROUP BY date(DateColumn)
ORDER BY DateColumn DESC
LIMIT 2);
答案 0 :(得分:2)
使用ROWID,ROWNUM和子查询这样的(未经测试)
delete from table WHERE rowid NOT IN
(SELECT rowid FROM (SELECT rowid
FROM Table
ORDER BY DateColumn DESC)
WHERE rownum <= 2);
注意&#34; double&#34;选择是必要的,因为rownum与顺序混淆,因为它在rownum之后应用
如果日期不重要而您只想保留两行(随机),则可以简化为
delete from table WHERE rowid NOT IN
(SELECT rowid FROM table WHERE rownum <= 2);
答案 1 :(得分:2)
这个出租车可以通过这个查询实现
DELETE FROM TABLE
WHERE ROWID NOT IN
(SELECT ROWIDS FROM (SELECT MIN(ROWID) ROWIDS
FROM TABLE
GROUP BY DATECOLUMN
ORDER BY DATECOLUMN DESC)
WHERE ROWNUM <= 2);
最内层的查询将从表中返回rowid的min,然后立即外部查询将在最外面的删除中仅选择要避免的两行。
答案 2 :(得分:0)
DELETE FROM Table
WHERE DateColumn NOT IN (SELECT DateColumn
FROM Table
Where ROWNUM < 3
GROUP BY date(DateColumn)
ORDER BY DateColumn DESC
);
我没有oracle客户端来运行查询。您可以在Oracle中使用ROWNUM,因为ORACLE中没有LIMIT