|
2 | 2 | title: LAST_QUERY_ID |
3 | 3 | --- |
4 | 4 |
|
5 | | -Returns the last query ID of query in current session, index can be (-1, 1, 1+2)..., out of range index will return empty string. |
| 5 | +Returns the ID of a query in the current session based on its order. |
| 6 | + |
| 7 | +:::note |
| 8 | +This function is currently supported only through the MySQL protocol, meaning you must connect to Databend using a MySQL protocol-compatible client for it to work. |
| 9 | +::: |
6 | 10 |
|
7 | 11 | ## Syntax |
8 | 12 |
|
9 | 13 | ```sql |
10 | 14 | LAST_QUERY_ID(<index>) |
11 | 15 | ``` |
| 16 | +`index` specifies the query order in the current session, accepting positive and negative numbers, with a default value of `-1`. |
| 17 | +- Positive indexes (starting from `1`) retrieve the nth query from the session start. |
| 18 | +- Negative indexes retrieve the nth query backward from the current query. |
| 19 | + - When `index` is `-1`, it returns the query ID of the current query. |
| 20 | + - To retrieve the previous query, set `index` to `-2`. |
| 21 | +- NULL is returned if an index exceeds the query history. |
12 | 22 |
|
13 | 23 | ## Examples |
14 | 24 |
|
15 | | -```sql |
16 | | -SELECT LAST_QUERY_ID(-1); |
| 25 | +This example runs three simple queries in a new session, then uses both positive and negative indexes to retrieve the query ID of `SELECT 3`: |
| 26 | + |
| 27 | +| | Positive | Negative | |
| 28 | +|----------------------------------------------|----------|----------| |
| 29 | +| `SELECT 1` | 1 | -4 | |
| 30 | +| `SELECT 2` | 2 | -3 | |
| 31 | +| `SELECT 3` | 3 | -2 | |
| 32 | +| `SELECT LAST_QUERY_ID(-2), LAST_QUERY_ID(3)` | 4 | -1 | |
| 33 | + |
| 34 | +```bash |
| 35 | +MacBook-Air:~ eric$ mysql -u root -h 127.0.0.1 -P 3307 |
| 36 | +Welcome to the MySQL monitor. Commands end with ; or \g. |
| 37 | +Your MySQL connection id is 9 |
| 38 | +Server version: 8.0.90-v1.2.720-nightly-2280cc9480(rust-1.85.0-nightly-2025-04-08T04:40:36.379825500Z) 0 |
| 39 | + |
| 40 | +Copyright (c) 2000, 2025, Oracle and/or its affiliates. |
| 41 | + |
| 42 | +Oracle is a registered trademark of Oracle Corporation and/or its |
| 43 | +affiliates. Other names may be trademarks of their respective |
| 44 | +owners. |
| 45 | + |
| 46 | +Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. |
| 47 | + |
| 48 | +mysql> select 1; |
| 49 | ++------+ |
| 50 | +| 1 | |
| 51 | ++------+ |
| 52 | +| 1 | |
| 53 | ++------+ |
| 54 | +1 row in set (0.02 sec) |
| 55 | +Read 1 rows, 1.00 B in 0.004 sec., 264.46 rows/sec., 264.46 B/sec. |
| 56 | + |
| 57 | +mysql> select 2; |
| 58 | ++------+ |
| 59 | +| 2 | |
| 60 | ++------+ |
| 61 | +| 2 | |
| 62 | ++------+ |
| 63 | +1 row in set (0.01 sec) |
| 64 | +Read 1 rows, 1.00 B in 0.003 sec., 366.94 rows/sec., 366.94 B/sec. |
| 65 | + |
| 66 | +mysql> select 3; |
| 67 | ++------+ |
| 68 | +| 3 | |
| 69 | ++------+ |
| 70 | +| 3 | |
| 71 | ++------+ |
| 72 | +1 row in set (0.01 sec) |
| 73 | +Read 1 rows, 1.00 B in 0.003 sec., 373.16 rows/sec., 373.16 B/sec. |
| 74 | + |
| 75 | +mysql> SELECT LAST_QUERY_ID(-2), LAST_QUERY_ID(3); |
| 76 | ++--------------------------------------+--------------------------------------+ |
| 77 | +| last_query_id(- 2) | last_query_id(3) | |
| 78 | ++--------------------------------------+--------------------------------------+ |
| 79 | +| 74dd6dca-f9b0-44cd-99f4-ac7d11d47fee | 74dd6dca-f9b0-44cd-99f4-ac7d11d47fee | |
| 80 | ++--------------------------------------+--------------------------------------+ |
| 81 | +1 row in set (0.02 sec) |
| 82 | +Read 1 rows, 1.00 B in 0.006 sec., 167.95 rows/sec., 167.95 B/sec. |
| 83 | +``` |
| 84 | +
|
| 85 | +This example demonstrates that the function returns the query ID of the current query when `<index>` is `-1`: |
| 86 | +
|
| 87 | +```bash |
| 88 | +MacBook-Air:~ eric$ mysql -u root -h 127.0.0.1 -P 3307 |
| 89 | +Welcome to the MySQL monitor. Commands end with ; or \g. |
| 90 | +Your MySQL connection id is 10 |
| 91 | +Server version: 8.0.90-v1.2.720-nightly-2280cc9480(rust-1.85.0-nightly-2025-04-08T04:40:36.379825500Z) 0 |
| 92 | + |
| 93 | +Copyright (c) 2000, 2025, Oracle and/or its affiliates. |
| 94 | + |
| 95 | +Oracle is a registered trademark of Oracle Corporation and/or its |
| 96 | +affiliates. Other names may be trademarks of their respective |
| 97 | +owners. |
| 98 | + |
| 99 | +Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. |
| 100 | + |
| 101 | +mysql> SELECT LAST_QUERY_ID(-1), LAST_QUERY_ID(); |
| 102 | ++--------------------------------------+--------------------------------------+ |
| 103 | +| last_query_id(- 1) | last_query_id() | |
| 104 | ++--------------------------------------+--------------------------------------+ |
| 105 | +| 5a1afbc2-dc16-4b69-a0e6-615e0b970cb1 | 5a1afbc2-dc16-4b69-a0e6-615e0b970cb1 | |
| 106 | ++--------------------------------------+--------------------------------------+ |
| 107 | +1 row in set (0.01 sec) |
| 108 | +Read 1 rows, 1.00 B in 0.003 sec., 393.68 rows/sec., 393.68 B/sec. |
| 109 | + |
| 110 | +mysql> SELECT LAST_QUERY_ID(-2); |
| 111 | ++--------------------------------------+ |
| 112 | +| last_query_id(- 2) | |
| 113 | ++--------------------------------------+ |
| 114 | +| 5a1afbc2-dc16-4b69-a0e6-615e0b970cb1 | |
| 115 | ++--------------------------------------+ |
| 116 | +1 row in set (0.01 sec) |
| 117 | +Read 1 rows, 1.00 B in 0.003 sec., 381.61 rows/sec., 381.61 B/sec. |
| 118 | + |
| 119 | +mysql> SELECT LAST_QUERY_ID(1); |
| 120 | ++--------------------------------------+ |
| 121 | +| last_query_id(1) | |
| 122 | ++--------------------------------------+ |
| 123 | +| 5a1afbc2-dc16-4b69-a0e6-615e0b970cb1 | |
| 124 | ++--------------------------------------+ |
| 125 | +1 row in set (0.01 sec) |
| 126 | +Read 1 rows, 1.00 B in 0.003 sec., 353.63 rows/sec., 353.63 B/sec. |
| 127 | +``` |
| 128 | +
|
| 129 | +When the `index` exceeds the query history, NULL is returned. |
17 | 130 |
|
18 | | -┌──────────────────────────────────────┐ |
19 | | -│ last_query_id((- 1)) │ |
20 | | -├──────────────────────────────────────┤ |
21 | | -│ a6f615c6-5bad-4863-8558-afd01889448c │ |
22 | | -└──────────────────────────────────────┘ |
| 131 | +```bash |
| 132 | +mysql> SELECT LAST_QUERY_ID(-100), LAST_QUERY_ID(100); |
| 133 | ++----------------------+--------------------+ |
| 134 | +| last_query_id(- 100) | last_query_id(100) | |
| 135 | ++----------------------+--------------------+ |
| 136 | +| | | |
| 137 | ++----------------------+--------------------+ |
| 138 | +1 row in set (0.02 sec) |
| 139 | +Read 1 rows, 1.00 B in 0.008 sec., 128.69 rows/sec., 128.69 B/sec. |
23 | 140 | ``` |
0 commit comments