如何检索数据库中最后插入的值?

时间:2022-07-02 02:40:02

Can anybody tell me the query for last inserted value in the column of the database.

任何人都可以告诉我在数据库的列中查询最后插入的值。

The problem is that inserted value can be placed in inner rows of the database after using ASC or DESC so I cant use the method. If anybody has the solution please tell me.

问题是在使用ASC或DESC之后插入的值可以放在数据库的内部行中,所以我不能使用该方法。如果有人有解决方案,请告诉我。

2 个解决方案

#1


12  

You will need to use a TIMESTAMP data column in order to keep track of insertion time. Unfortunately, due to inherent race conditions, using auto-incrementing primary keys will not work in this case. Simply add the entry timestamp as a data column and retrieve with ORDER BY time_stamp DESC LIMIT 1 to get the last record. If you're feeling really defensive, aim to include conditions in the WHERE clause that are unique to the original INSERT call (i.e., WHERE ID = x AND KEY = y)

您需要使用TIMESTAMP数据列以跟踪插入时间。不幸的是,由于固有的竞争条件,在这种情况下使用自动递增主键不起作用。只需将条目时间戳添加为数据列,然后使用ORDER BY time_stamp DESC LIMIT 1进行检索以获取最后一条记录。如果你感觉真的是防御性的,那么就要在WHERE子句中包含原始INSERT调用所特有的条件(即,WHERE ID = x AND KEY = y)

#2


2  

I second @Daniel Li's comments.

我是第二个@Daniel Li的评论。

In every persistent table that I create, I have the following columns:

在我创建的每个持久表中,我有以下列:

  • id which is the first column and auto-incremented/identity/serial column
  • id是第一列和自动递增/标识/串行列
  • CreatedBy which defaults to the user ("default system_user" in SQL Server) so I know who updated the column
  • CreatedBy默认为用户(SQL Server中的“default system_user”),因此我知道谁更新了该列
  • CreatedAt which defaults to the datetime of creation ("default getdate() in SQL Server).
  • CreatedAt默认为创建的日期时间(“SQL Server中的默认getdate()”)。

(The exact syntax varies depending on the databse.)

(确切的语法因数据库而异。)

With the exception of race conditions, I can find the last inserted row by doing "select * from table order by 1 desc".

除了竞争条件之外,我可以通过“select * from table order by 1 desc”找到最后插入的行。

Although these take extra space, I've found that they more than pay back by being able to resolve issues over time.

虽然这些占用了额外的空间,但我发现它们不仅仅是能够随着时间的推移解决问题而得到回报。

#1


12  

You will need to use a TIMESTAMP data column in order to keep track of insertion time. Unfortunately, due to inherent race conditions, using auto-incrementing primary keys will not work in this case. Simply add the entry timestamp as a data column and retrieve with ORDER BY time_stamp DESC LIMIT 1 to get the last record. If you're feeling really defensive, aim to include conditions in the WHERE clause that are unique to the original INSERT call (i.e., WHERE ID = x AND KEY = y)

您需要使用TIMESTAMP数据列以跟踪插入时间。不幸的是,由于固有的竞争条件,在这种情况下使用自动递增主键不起作用。只需将条目时间戳添加为数据列,然后使用ORDER BY time_stamp DESC LIMIT 1进行检索以获取最后一条记录。如果你感觉真的是防御性的,那么就要在WHERE子句中包含原始INSERT调用所特有的条件(即,WHERE ID = x AND KEY = y)

#2


2  

I second @Daniel Li's comments.

我是第二个@Daniel Li的评论。

In every persistent table that I create, I have the following columns:

在我创建的每个持久表中,我有以下列:

  • id which is the first column and auto-incremented/identity/serial column
  • id是第一列和自动递增/标识/串行列
  • CreatedBy which defaults to the user ("default system_user" in SQL Server) so I know who updated the column
  • CreatedBy默认为用户(SQL Server中的“default system_user”),因此我知道谁更新了该列
  • CreatedAt which defaults to the datetime of creation ("default getdate() in SQL Server).
  • CreatedAt默认为创建的日期时间(“SQL Server中的默认getdate()”)。

(The exact syntax varies depending on the databse.)

(确切的语法因数据库而异。)

With the exception of race conditions, I can find the last inserted row by doing "select * from table order by 1 desc".

除了竞争条件之外,我可以通过“select * from table order by 1 desc”找到最后插入的行。

Although these take extra space, I've found that they more than pay back by being able to resolve issues over time.

虽然这些占用了额外的空间,但我发现它们不仅仅是能够随着时间的推移解决问题而得到回报。