Skip to content

LAST_QUERY_ID()

Grammar Description

Returns the ID of the specified query in the current session. If no query is specified, the most recently executed query is returned.

Grammar Structure

> LAST_QUERY_ID([ <num> ])

Parameter definition

-num: Specify the query to be returned based on the location of the query in the current session. The default is -1.

User definition

num is a positive number, which starts with the first query executed in the session. For example:

  • `LAST_QUERY_ID(1): Returns the first query.

  • `LAST_QUERY_ID(2): Returns the second query.

  • LAST_QUERY_ID(6): Returns the sixth query.

num is a negative number, that is, it starts with the most recently executed query in the session. For example:

  • LAST_QUERY_ID(-1): Returns the most recently executed query (equivalent to LAST_QUERY_ID()).

  • LAST_QUERY_ID(-2): Returns the second query that was recently executed.

Example

mysql> SELECT LAST_QUERY_ID(-1);
+---------------------------------------------+
| last_query_id(-1) |
+---------------------------------------------+
| af974680-b1b5-11ed-8eb9-5ad2460dea4f |
+---------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT LAST_QUERY_ID();
+---------------------------------------------+
| last_query_id() |
+---------------------------------------------+
| 550e4d44-b1b5-11ed-8eb9-5ad2460dea4f |
+---------------------------------------------+
1 row in set (0.00 sec)