試試這個:
with a as
(
select ROW_NUMBER() over(order by name) as ordinal, * from test
)
delete from a where a.ordinal > 10;
相關:http://www.ienablemuch.com/2012/03/possible-in-sql-server-deleting-any-row.html
的樣本數據:
CREATE TABLE [beatles]
([name] varchar(14));
INSERT INTO [beatles]
([name])
VALUES
('john'),
('paul'),
('george'),
('ringo'),
('pete'),
('brian'),
('george martin');
查詢:
with a as
(
select *, row_number() over(order by name) ordinal
from beatles
)
delete from a
where ordinal > 4;
select * from beatles;
之前刪除:
NAME
brian
george
george martin
john
paul
pete
ringo
刪除後:
NAME
brian
george
george martin
john
現場測試:http://www.sqlfiddle.com/#!3/0adcf/6
會不會選擇highscore_ID子查詢基礎上的ID號碼,而不是選擇的前10分的id實際的高分? – Euthyphro
那麼你必須爲你的子查詢返回Highscore_Id,但是,那麼訂單就不適合。你必須在你的表格中添加一個普通的等級並刪除所有等級> 11),那麼它就可以工作。 – YvesR
謝謝。 「DELETE FROM SCORE WHERE difficulty = 1 AND Highscore_ID NOT IN(SELECT TOP 10 Highscore_ID FROM Scores WHERE difficulty = 1 ORDER BY highScore DESC);」 – Euthyphro