SQL 如何在SQL中选择表的最后一条记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5191503/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 09:33:48  来源:igfitidea点击:

How to select the last record of a table in SQL?

sqlsql-serversql-server-2008

提问by Tassisto

This is a sample code to select all records from a table. Can someone show me how to select the last record of that table?

这是从表中选择所有记录的示例代码。有人可以告诉我如何选择该表的最后一条记录吗?

select * from table

When I use: SELECT * FROM TABLE ORDER BY ID DESC LIMITI get this error: Line 1: Incorrect syntax near 'LIMIT'. This is the code I use:

当我使用时:SELECT * FROM TABLE ORDER BY ID DESC LIMIT我收到此错误:第 1 行:'LIMIT' 附近的语法不正确。这是我使用的代码:

private void LastRecord()
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HELPDESK_OUTLOOKConnectionString3"].ToString());

    conn.Open();
    SqlDataReader myReader = null;
    SqlCommand myCommand = new SqlCommand("SELECT * FROM HD_AANVRAGEN ORDER BY " +
                "aanvraag_id DESC LIMIT 1", conn);
    myReader = myCommand.ExecuteReader();
    while (myReader.Read())
    {
        TextBox1.Text = (myReader["aanvraag_id"].ToString());
        TextBox1.Text += (myReader["wijziging_nummer"].ToString());
        TextBox1.Text += (myReader["melding_id"].ToString());
        TextBox1.Text += (myReader["aanvraag_titel"].ToString());
        TextBox1.Text += (myReader["aanvraag_omschrijving"].ToString());
        TextBox1.Text += (myReader["doorlooptijd_id"].ToString());
        TextBox1.Text += (myReader["rapporteren"].ToString());
        TextBox1.Text += (myReader["werknemer_id"].ToString());
        TextBox1.Text += (myReader["outlook_id"].ToString());
    }
}

回答by Adriaan Stander

Without any further information, which Database etc the best we can do is something like

没有任何进一步的信息,我们可以做的最好的数据库等是这样的

Sql Server

数据库服务器

SELECT TOP 1 * FROM Table ORDER BY ID DESC

MySql

数据库

SELECT * FROM Table ORDER BY ID DESC LIMIT 1

回答by Neil Knight

Assuming you have an Id column:

假设您有一个 Id 列:

SELECT TOP 1 *
  FROM table
 ORDER
    BY Id DESC;

Also, this will work on SQL Server. I think that MySQL you might need to use:

此外,这将适用于 SQL Server。我认为您可能需要使用 MySQL:

SELECT *
  FROM table
 ORDER
    BY Id DESC
 LIMIT 1

But, I'm not 100% sure about this.

但是,我不是 100% 确定这一点。

EDIT

编辑

Looking at the other answers, I'm now 100% confident that I'm correct with the MySQL statement :o)

查看其他答案,我现在 100% 确信我对 MySQL 语句是正确的:o)

EDIT

编辑

Just seen your latest comment. You could do:

刚看到你的最新评论。你可以这样做:

SELECT MAX(Id)
  FROM table

This will get you the highest Id number.

这将为您提供最高的 ID 号。

回答by Ricardo Fercher

to get the last rowof a SQL-Databaseuse this sql string:

要获取SQL 数据库的最后一行,请使用以下 sql 字符串:

SELECT * FROM TableName WHERE id=(SELECT max(id) FROM TableName);

Output:

输出:

Last Line of your db!

你数据库的最后一行!

回答by Simon

SELECT * FROM TABLE ORDER BY ID DESC LIMIT 1

Yes this is mysql, SQL Server:

是的,这是 mysql,SQL Server:

SELECT TOP 1 * FROM Table ORDER BY ID DESC

回答by Simon

SELECT * FROM table ORDER BY Id DESC LIMIT 1

回答by jeje

The last is just the first when you reverse your ordering.

当您颠倒顺序时,最后一个只是第一个。

回答by GeeDee

In Oracle, you can do:

在 Oracle 中,您可以执行以下操作:

SELECT *
FROM (SELECT EMP.*,ROWNUM FROM EMP ORDER BY ROWNUM DESC)
WHERE ROWNUM=1;

This is one of the possible ways.

这是可能的方法之一。

回答by Panjas51

select ADU.itemid, ADU.startdate, internalcostprice 
from ADUITEMINTERNALCOSTPRICE ADU

right join

   (select max(STARTDATE) as Max_date, itemid 
   from ADUITEMINTERNALCOSTPRICE
   group by itemid) as A

on A.ITEMID = ADU.ITEMID
and startdate= Max_date

回答by Jenna Leaf

It is always a good practice in your table design to have an automatic row identifier, such as

在您的表设计中使用自动行标识符始终是一个好习惯,例如

 [RowID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL

, then you can identify your last row by

,然后您可以通过以下方式识别您的最后一行

 select * from yourTable where rowID =  @@IDENTITY 

回答by Chanuka Fernando

I think this should do it.

我认为应该这样做。

declare @x int;
select @x = max(id) from table_name;
select * from where id = @x;