获取MySQL表的最后一个条目

时间:2022-09-25 16:13:02

I'm basically trying to make a "goal" bar. The goal is determined by getting the last entry I made in a MySQL table. I want to get the ID of the last entry therefore.

我基本上是在尝试设立一个“目标”栏。目标是通过获取我在MySQL表中创建的最后一个条目来确定的。因此,我想获得最后一项的ID。

How do I get the last entry in the table and then get the id from that last entry?

如何获取表中的最后一项,然后从最后一项中获取id ?

(Using PHP)

(使用PHP)

7 个解决方案

#1


28  

To get the greatest id:

获得最佳id:

SELECT MAX(id) FROM mytable

Then to get the row:

然后得到一排:

SELECT * FROM mytable WHERE id = ???

Or, you could do it all in one query:

或者,你可以在一个查询中完成:

SELECT * FROM mytable ORDER BY id DESC LIMIT 1

#2


7  

you can use LAST_INSERT_ID() function. example:

可以使用LAST_INSERT_ID()函数。例子:

$sql = "SELECT * FROM mytable WHERE id = LAST_INSERT_ID()";

#3


6  

you can use this query to get the results you want with this sql query as used in this example:

您可以使用此查询获取您想要的sql查询结果,如本例所示:

$sql = "SELECT user_id FROM my_users_table ORDER BY user_id DESC LIMIT 0,1";

#4


2  

To do this reliably, you must have some field in the table that you can examine to determine which is last. This could be a time stamp of when you added the record, a sequence number that continually increases (especially an auto-incrementing sequence number), etc.

要可靠地做到这一点,您必须在表中有一些字段,您可以对这些字段进行检查,以确定哪些字段是最后的字段。这可以是添加记录时的时间戳、连续增加的序列号(特别是自动递增的序列号)等等。

Then, let's suppose it's a sequence number called "rec_seq". You'd write something like:

然后,假设它是一个名为“rec_seq”的序列号。你可以这样写:

select * from my_table
where rec_seq=(select max(rec_seq) from my_table)

#5


2  

select all fields from table in reverse order and set limit 0,1 This will give you last result of table field data

从表中以反向顺序选择所有字段,并设置limit 0,1,这将给出表字段数据的最后结果

#6


2  

Get the latest entry of a user

获取用户的最新条目

'SELECT user_id FROM table_name WHERE user_id = '.$userid.' ORDER BY user_id DESC LIMIT 1';

#7


2  

if the field is auto-incremented then you can use LAST_INSERT_ID

如果字段是自动递增的,那么可以使用LAST_INSERT_ID

#1


28  

To get the greatest id:

获得最佳id:

SELECT MAX(id) FROM mytable

Then to get the row:

然后得到一排:

SELECT * FROM mytable WHERE id = ???

Or, you could do it all in one query:

或者,你可以在一个查询中完成:

SELECT * FROM mytable ORDER BY id DESC LIMIT 1

#2


7  

you can use LAST_INSERT_ID() function. example:

可以使用LAST_INSERT_ID()函数。例子:

$sql = "SELECT * FROM mytable WHERE id = LAST_INSERT_ID()";

#3


6  

you can use this query to get the results you want with this sql query as used in this example:

您可以使用此查询获取您想要的sql查询结果,如本例所示:

$sql = "SELECT user_id FROM my_users_table ORDER BY user_id DESC LIMIT 0,1";

#4


2  

To do this reliably, you must have some field in the table that you can examine to determine which is last. This could be a time stamp of when you added the record, a sequence number that continually increases (especially an auto-incrementing sequence number), etc.

要可靠地做到这一点,您必须在表中有一些字段,您可以对这些字段进行检查,以确定哪些字段是最后的字段。这可以是添加记录时的时间戳、连续增加的序列号(特别是自动递增的序列号)等等。

Then, let's suppose it's a sequence number called "rec_seq". You'd write something like:

然后,假设它是一个名为“rec_seq”的序列号。你可以这样写:

select * from my_table
where rec_seq=(select max(rec_seq) from my_table)

#5


2  

select all fields from table in reverse order and set limit 0,1 This will give you last result of table field data

从表中以反向顺序选择所有字段,并设置limit 0,1,这将给出表字段数据的最后结果

#6


2  

Get the latest entry of a user

获取用户的最新条目

'SELECT user_id FROM table_name WHERE user_id = '.$userid.' ORDER BY user_id DESC LIMIT 1';

#7


2  

if the field is auto-incremented then you can use LAST_INSERT_ID

如果字段是自动递增的,那么可以使用LAST_INSERT_ID