如何使用 SQL Server 读取最后一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/177323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read the last row with SQL Server
提问by rp.
What is the most efficient way to read the last row with SQL Server?
使用 SQL Server 读取最后一行的最有效方法是什么?
The table is indexed on a unique key -- the "bottom" key values represent the last row.
该表以唯一键为索引——“底部”键值代表最后一行。
回答by EggyBach
If you're using MS SQL, you can try:
如果您使用的是 MS SQL,您可以尝试:
SELECT TOP 1 * FROM table_Name ORDER BY unique_column DESC
回答by Adam Pierce
select whatever,columns,you,want from mytable
where mykey=(select max(mykey) from mytable);
回答by willurd
You'll need some sort of uniquely identifying column in your table, like an auto-filling primary key or a datetime column (preferably the primary key). Then you can do this:
您需要在表中使用某种唯一标识列,例如自动填充主键或日期时间列(最好是主键)。然后你可以这样做:
SELECT * FROM table_name ORDER BY unique_column DESC LIMIT 1
The ORDER BY column
tells it to rearange the results according to that column's data, and the DESC
tells it to reverse the results (thus putting the last one first). After that, the LIMIT 1
tells it to only pass back one row.
该ORDER BY column
告诉它根据该列的数据,以rearange结果,并且DESC
告诉它扭转结果(从而把最后一个第一)。之后,LIMIT 1
告诉它只传回一行。
回答by lionelmessi
If some of your id are in order, i am assuming there will be some order in your db
如果你的一些 id 是有序的,我假设你的数据库中会有一些订单
SELECT * FROM TABLE WHERE ID = (SELECT MAX(ID) FROM TABLE)
SELECT * FROM TABLE WHERE ID = (SELECT MAX(ID) FROM TABLE)
回答by ConsiderItDone
I think below query will work for SQL Server with maximum performance without any sortable column
我认为下面的查询将适用于具有最高性能的 SQL Server,无需任何可排序的列
SELECT * FROM table
WHERE ID not in (SELECT TOP (SELECT COUNT(1)-1
FROM table)
ID
FROM table)
Hope you have understood it... :)
希望你已经明白了... :)
回答by Neha Verma
I tried using last in sql query in SQl server 2008 but it gives this err: " 'last' is not a recognized built-in function name."
我尝试在 SQL Server 2008 中使用 last in sql 查询,但它给出了这个错误:“'last' 不是公认的内置函数名称。”
So I ended up using :
所以我最终使用了:
select max(WorkflowStateStatusId) from WorkflowStateStatus
to get the Id of the last row. One could also use
获取最后一行的 ID。一个也可以使用
Declare @i int
set @i=1
select WorkflowStateStatusId from Workflow.WorkflowStateStatus
where WorkflowStateStatusId not in (select top (
(select count(*) from Workflow.WorkflowStateStatus) - @i ) WorkflowStateStatusId from .WorkflowStateStatus)
回答by Ashish pathak
Try this
尝试这个
SELECT id from comission_fees ORDER BY id DESC LIMIT 1
回答by Gil Gomes
You can use last_value: SELECT LAST_VALUE(column) OVER (PARTITION BY column ORDER BY column)...
您可以使用 last_value: SELECT LAST_VALUE(column) OVER (PARTITION BY column ORDER BY column)...
I test it at one of my databases and it worked as expected.
我在我的一个数据库中对其进行了测试,它按预期工作。
You can also check de documentation here: https://msdn.microsoft.com/en-us/library/hh231517.aspx
您还可以在此处查看文档:https: //msdn.microsoft.com/en-us/library/hh231517.aspx
回答by Jignesh Bhayani
OFFSET
and FETCH NEXT
are a feature of SQL Server 2012 to achieve SQL paging while displaying results.
OFFSET
并且FETCH NEXT
是 SQL Server 2012 的一项功能,可以在显示结果的同时实现 SQL 分页。
The OFFSET
argument is used to decide the starting row to return rows from a result and FETCH
argument is used to return a set of number of rows.
该OFFSET
参数用于从结果决定开始行返回行和FETCH
参数用于返回一组行的数量。
SELECT *
FROM table_name
ORDER BY unique_column desc
OFFSET 0 Row
FETCH NEXT 1 ROW ONLY
回答by Jignesh Bhayani
In order to retrieve the last row of a table for MS SQL database 2005, You can use the following query:
为了检索 MS SQL 数据库 2005 表的最后一行,您可以使用以下查询:
select top 1 column_name from table_name order by column_name desc;
Note:To get the first row of the table for MS SQL database 2005, You can use the following query:
注意:要获取 MS SQL 数据库 2005 表的第一行,您可以使用以下查询:
select top 1 column_name from table_name;