SQL:如何从表中检索最后n条记录而不使用任何字段/列 [英] SQL: How to retrieve last n records from table without using any field/column

查看:83
本文介绍了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 that TOP 5 without ORDER 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 using ORDER BY clause. So no there is no way to retrieve last 5 rows without using any columns in the query. Always use ORDER BY with TOP to get predictable results!


这篇关于SQL:如何从表中检索最后n条记录而不使用任何字段/列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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