Programing

SQL에서 테이블의 마지막 레코드를 선택하는 방법은 무엇입니까?

lottogame 2020. 8. 22. 11:29
반응형

SQL에서 테이블의 마지막 레코드를 선택하는 방법은 무엇입니까?


테이블에서 모든 레코드를 선택하는 샘플 코드입니다. 누군가가 그 테이블의 마지막 레코드를 선택하는 방법을 보여줄 수 있습니까?

select * from table

사용할 때 : 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());
    }
}

추가 정보없이, 우리가 할 수있는 최선의 데이터베이스 등은 다음과 같습니다.

SQL 서버

SELECT TOP 1 * FROM Table ORDER BY ID DESC

MySql

SELECT * FROM Table ORDER BY ID DESC LIMIT 1

Id 열이 있다고 가정합니다.

SELECT TOP 1 *
  FROM table
 ORDER
    BY Id DESC;

또한 이것은 SQL Server에서 작동합니다. MySQL을 사용해야 할 수도 있다고 생각합니다.

SELECT *
  FROM table
 ORDER
    BY Id DESC
 LIMIT 1

그러나 나는 이것에 대해 100 % 확신하지 못합니다.

편집하다

다른 답변을 살펴보면 이제 MySQL 문이 정확하다고 100 % 확신합니다. : o)

편집하다

최근 댓글을 확인했습니다. 다음과 같이 할 수 있습니다.

SELECT MAX(Id)
  FROM table

이렇게하면 가장 높은 ID 번호를 얻을 수 있습니다.


SELECT * FROM TABLE ORDER BY ID DESC LIMIT 1

예 이것은 mysql, SQL Server입니다.

SELECT TOP 1 * FROM Table ORDER BY ID DESC

to get the last row of a SQL-Database use this sql string:

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

Output:

Last Line of your db!


SELECT * FROM table ORDER BY Id DESC LIMIT 1

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


In Oracle, you can do:

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

This is one of the possible ways.


$sql="SELECT tot_visit FROM visitors WHERE date = DATE(NOW()) - 1 into @s                
$conn->query($sql);
$sql = "INSERT INTO visitors (nbvisit_day,date,tot_visit) VALUES (1,CURRENT_DATE,@s+1)";
$conn->query($sql);

You can also do something like this:

SELECT LAST (column_name) AS LAST_CUSTOMER FROM table_name;


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

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 

I think this should do it.

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

참고URL : https://stackoverflow.com/questions/5191503/how-to-select-the-last-record-of-a-table-in-sql

반응형