如何在SQL中以单行显示最后7条记录 [英] How to display last 7 record in single row in SQL

查看:70
本文介绍了如何在SQL中以单行显示最后7条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有一张桌子,里面有多条记录。我想将最后7行的最后7条记录追溯到一行。我该怎么写查询。



我尝试过:



i已尝试过子查询但不起作用

hello i have a table in which there are multiple record. i want to retrive last 7 record from last 7 row in to a single row. how can i write query.

What I have tried:

i have tried sub query but doesnt work

推荐答案

如果您使用的是SQL Server,那么您可以执行以下操作 -

If you are using SQL Server then you can do something like following-
SELECT coulmn1,column2,...
FROM
(
    SELECT ROW_NUMBER() OVER (ORDER BY coulmn2 DESC) slno, * --where column2 is the column by which you can distinguish which are last records
    FROM MyTable
) as tbl
WHERE tbl.slno <= 7



Foll欠查询也应该有效,但是当你有大量的记录时可能会非常耗时。好的是它适用于所有主要的DBMS。


Following query should also work but can be time consuming when you have large no of records. Good thing is it should work for all major DBMS.

SELECT TOP 7 *
FROM MyTable
ORDER BY Column2 DESC





希望,它有帮助:)

如果您对此仍有疑问,请与我们联系。



Hope, it helps :)
Please let me know if you still have issue on this.


这篇关于如何在SQL中以单行显示最后7条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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