如何从表MySQL中获取最后一个插入的id [复制]

时间:2022-09-25 16:21:36

This question already has an answer here:

这个问题在这里已有答案:

I am running 1 script in php for that I need last inserted id in subscription table. By using that id I want to make notification note for that subscription.

我在php中运行了1个脚本,因为我需要在订阅表中输入最后一个id。通过使用该ID,我想为该订阅发出通知。

I used:

我用了:

SELECT LAST_INSERT_ID() FROM subscription

I am getting 0 instead of real last inserted value.

我得到0而不是真正的最后插入值。

6 个解决方案

#1


7  

If you use php to connect to mysql you can use mysql_insert_id() to point to last inserted id.

如果你使用php连接到mysql,你可以使用mysql_insert_id()指向最后插入的id。

Like this :

喜欢这个 :

mysql_query("INSERT INTO mytable (1, 2, 3, 'blah')");
$last_id = mysql_insert_id();

See this : mysql_insert_id()

看到这个:mysql_insert_id()

#2


4  

LAST_INSERT_ID() returns the last id from a previous insert statement. If you want the most recently inserted record and are using Auto Increment Prime keys, you can use the code below:

LAST_INSERT_ID()返回上一个insert语句的最后一个id。如果您想要最近插入的记录并使用自动增量Prime键,则可以使用以下代码:

SELECT MAX( id ) FROM subscription;

If you need to know what the NEXT id will be, you can get this from INFORMATION_SCHEMA

如果您需要知道NEXT id是什么,可以从INFORMATION_SCHEMA获取

SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'test'

mysql_insert_id

mysql_insert_id

#3


3  

This question has already been answered many times: MySQL: LAST_INSERT_ID() returns 0

这个问题已经多次回答:MySQL:LAST_INSERT_ID()返回0

You are using that function out of context. It will only work if you inserted a row immediately prior thusly:

您正在使用该功能脱离上下文。只有在您之前立即插入一行时它才会起作用:

INSERT INTO 'subscription' (name) VALUES ('John Smith');
SELECT LAST_INSERT_ID() FROM subscription

You can however select the row with the highest id, which logically would be the most recently added...

但是,您可以选择ID最高的行,逻辑上最新添加的行...

SELECT MAX( id ) FROM subscription;

The standard approach however is to simply call mysqli_insert_id or mysql_insert_id (depending on whether you are using the mysqli or mysql PHP library. I should add that the mysql library is very inadvisable to use since it is almost completely deprecated). Here's what the whole thing would ideally look like:

然而,标准方法是简单地调用mysqli_insert_id或mysql_insert_id(取决于您是使用mysqli还是mysql PHP库。我应该补充一点,因为几乎完全弃用了mysql库,所以使用它是非常不可取的)。这就是理想情况下整个事情的样子:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$mysqli->query("INSERT INTO 'subscription' (name) VALUES ('John Smith');");
printf ("New Record has id %d.\n", $mysqli->insert_id);
//Output is something like: New Record has id 999

If however you didn't insert a subscription in the same script as collecting the most recent row ID, use the 'select max' approach. This seems unlikely given that you mentioned '1 script'

但是,如果您没有在收集最新行ID的同一脚本中插入订阅,请使用“select max”方法。鉴于您提到'1脚本',这似乎不太可能

Also, if your ID's are non-consecutive, or you do not have an ID field, or you have row ID's higher than the one you just added you should probably consider a 'date_added' column to determine which one was really the latest. These scenarios are rather unlikely however.

此外,如果您的ID是非连续的,或者您没有ID字段,或者您的行ID高于您刚添加的ID,则应该考虑使用'date_added'列来确定哪一个是最新的。然而,这些情况不太可能。

#4


2  

this is the better approach 2 and 3 works but MAX(id) take more time to execute.

这是更好的方法2和3工作,但MAX(id)需要更多的时间来执行。

  1. SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'dbName' AND TABLE_NAME = 'tableName';

    SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA ='dbName'AND TABLE_NAME ='tableName';

  2. SELECT tableName.id FROM tableName ORDER BY tableName.id DESC LIMIT 0,1;

    SELECT tableName.id FROM tableName ORDER BY tableName.id DESC LIMIT 0,1;

  3. SELECT MAX( id ) FROM tableName;

    SELECT MAX(id)FROM tableName;

#5


1  

This will always give you the maximum id, which says the biggest number is the last inserted one

这将始终为您提供最大ID,即最大数字是最后一个插入的ID

 SELECT MAX(id) as MaximumID FROM subscription;

#6


1  

$last_id=mysql_query("SELECT id FROM `table_name` ORDER BY id DESC LIMIT 0 , 1" );
$row=mysql_fetch_assoc($last_id);
echo $row['id'];

Replace id by your id field name in database and table_name by your table name.

使用表名替换数据库和table_name中的id字段名称。

#1


7  

If you use php to connect to mysql you can use mysql_insert_id() to point to last inserted id.

如果你使用php连接到mysql,你可以使用mysql_insert_id()指向最后插入的id。

Like this :

喜欢这个 :

mysql_query("INSERT INTO mytable (1, 2, 3, 'blah')");
$last_id = mysql_insert_id();

See this : mysql_insert_id()

看到这个:mysql_insert_id()

#2


4  

LAST_INSERT_ID() returns the last id from a previous insert statement. If you want the most recently inserted record and are using Auto Increment Prime keys, you can use the code below:

LAST_INSERT_ID()返回上一个insert语句的最后一个id。如果您想要最近插入的记录并使用自动增量Prime键,则可以使用以下代码:

SELECT MAX( id ) FROM subscription;

If you need to know what the NEXT id will be, you can get this from INFORMATION_SCHEMA

如果您需要知道NEXT id是什么,可以从INFORMATION_SCHEMA获取

SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'test'

mysql_insert_id

mysql_insert_id

#3


3  

This question has already been answered many times: MySQL: LAST_INSERT_ID() returns 0

这个问题已经多次回答:MySQL:LAST_INSERT_ID()返回0

You are using that function out of context. It will only work if you inserted a row immediately prior thusly:

您正在使用该功能脱离上下文。只有在您之前立即插入一行时它才会起作用:

INSERT INTO 'subscription' (name) VALUES ('John Smith');
SELECT LAST_INSERT_ID() FROM subscription

You can however select the row with the highest id, which logically would be the most recently added...

但是,您可以选择ID最高的行,逻辑上最新添加的行...

SELECT MAX( id ) FROM subscription;

The standard approach however is to simply call mysqli_insert_id or mysql_insert_id (depending on whether you are using the mysqli or mysql PHP library. I should add that the mysql library is very inadvisable to use since it is almost completely deprecated). Here's what the whole thing would ideally look like:

然而,标准方法是简单地调用mysqli_insert_id或mysql_insert_id(取决于您是使用mysqli还是mysql PHP库。我应该补充一点,因为几乎完全弃用了mysql库,所以使用它是非常不可取的)。这就是理想情况下整个事情的样子:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$mysqli->query("INSERT INTO 'subscription' (name) VALUES ('John Smith');");
printf ("New Record has id %d.\n", $mysqli->insert_id);
//Output is something like: New Record has id 999

If however you didn't insert a subscription in the same script as collecting the most recent row ID, use the 'select max' approach. This seems unlikely given that you mentioned '1 script'

但是,如果您没有在收集最新行ID的同一脚本中插入订阅,请使用“select max”方法。鉴于您提到'1脚本',这似乎不太可能

Also, if your ID's are non-consecutive, or you do not have an ID field, or you have row ID's higher than the one you just added you should probably consider a 'date_added' column to determine which one was really the latest. These scenarios are rather unlikely however.

此外,如果您的ID是非连续的,或者您没有ID字段,或者您的行ID高于您刚添加的ID,则应该考虑使用'date_added'列来确定哪一个是最新的。然而,这些情况不太可能。

#4


2  

this is the better approach 2 and 3 works but MAX(id) take more time to execute.

这是更好的方法2和3工作,但MAX(id)需要更多的时间来执行。

  1. SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'dbName' AND TABLE_NAME = 'tableName';

    SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA ='dbName'AND TABLE_NAME ='tableName';

  2. SELECT tableName.id FROM tableName ORDER BY tableName.id DESC LIMIT 0,1;

    SELECT tableName.id FROM tableName ORDER BY tableName.id DESC LIMIT 0,1;

  3. SELECT MAX( id ) FROM tableName;

    SELECT MAX(id)FROM tableName;

#5


1  

This will always give you the maximum id, which says the biggest number is the last inserted one

这将始终为您提供最大ID,即最大数字是最后一个插入的ID

 SELECT MAX(id) as MaximumID FROM subscription;

#6


1  

$last_id=mysql_query("SELECT id FROM `table_name` ORDER BY id DESC LIMIT 0 , 1" );
$row=mysql_fetch_assoc($last_id);
echo $row['id'];

Replace id by your id field name in database and table_name by your table name.

使用表名替换数据库和table_name中的id字段名称。