SQL:如何从表中检索最后n条记录而不使用任何字段/列 [英] SQL: How to retrieve last n records from table without using any field/column
本文介绍了SQL:如何从表中检索最后n条记录而不使用任何字段/列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
像这样,
检索前n行
SELECT TOP(5)* FROM table1
解决方案
< blockquote>请注意 TOP 5
没有 ORDER BY
可能会返回任何 5行表。要返回第一个或最后行,您需要定义 first 和 last 的含义。您可以使用 ORDER BY
子句执行此操作。因此,如果不使用查询中的任何列,则无法检索最后5行。 始终使用 ORDER BY
与 TOP
以获得可预测的结果!
like this,
To retrieve top n rows
SELECT TOP(5)* FROM table1
解决方案
Please note thatTOP 5
withoutORDER BY
may return any 5 rows from the table. In order to return first or last rows you need to define what first and last means. You do that usingORDER BY
clause. So no there is no way to retrieve last 5 rows without using any columns in the query. Always useORDER BY
withTOP
to get predictable results!
这篇关于SQL:如何从表中检索最后n条记录而不使用任何字段/列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文