如何在不排序的情况下选择表中的最后 5 行? [英] How do I select last 5 rows in a table without sorting?

查看:26
本文介绍了如何在不排序的情况下选择表中的最后 5 行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 SQL Server 中的一个表中选择最后 5 条记录,而不是按升序或降序排列该表.

I want to select the last 5 records from a table in SQL Server without arranging the table in ascending or descending order.

推荐答案

这几乎是我写过的最奇怪的查询,但我很确定它无需排序即可从表中获取最后 5"行:

This is just about the most bizarre query I've ever written, but I'm pretty sure it gets the "last 5" rows from a table without ordering:

select * 
from issues
where issueid not in (
    select top (
        (select count(*) from issues) - 5
    ) issueid
    from issues
)

请注意,这利用了 SQL Server 2005 将值传递到top"子句的能力 - 它不适用于 SQL Server 2000.

Note that this makes use of SQL Server 2005's ability to pass a value into the "top" clause - it doesn't work on SQL Server 2000.

这篇关于如何在不排序的情况下选择表中的最后 5 行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆