如何在SQL中选择表的最后一条记录? [英] How to select the last record of a table in SQL?

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

问题描述

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

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

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

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

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());
    }
}

推荐答案

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

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

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

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