©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
This website works better with Javascript
The MyWAY SQL Manager can be downloaded here for free: as PHP installation file as GZIP file as ZIP archive
If the download from this page does not work, you can also download the program here (Virus checked at heise.de).
By downloading you accept the General Terms and Conditions
The program MyWAY SQL-Manager only works with Javascript
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMariaDB

MariaDB LASTVAL SQL Function - PREVIOUS VALUE FOR Sequence, Last value from sequence

LASTVALSyntax:LASTVAL(Sequence)
Return value:INTEGER
Function type:Sequence function
Function Description

The function LASTVAL() returns the last value in a current connection after a NEXTVAL in a SEQUENCE.

Synonym for "PREVIOUS VALUE FOR sequence".

Available from MariaDB version 10.3.

Example 1: NEXT VALUE FOR and PEVIOUS VALUE FOR one after the other in a current connection

 1
 2
 3
 4
 5

select next VALUE FOR `my_sequence` as result;
select Previous VALUE FOR `my_sequence` as result;


result
bigint(20)
23
result
bigint(20)
23

Example 2: NEXTVAL() and LASTVAL() one after the other in a current connection

 1
 2
 3
 4
 5

select nextval(`my_sequence`as result;
select lastval(`my_sequence`as result;


result
bigint(20)
24
result
bigint(20)
24

Example 3: LASTVAL() without NEXTVAL() in a current connection

 1
 2
 3
 4

select lastval(`my_sequence`as result;


result
bigint(20)
NULL
The examples were created with the MyWAY SQL manager: Download

How to use the LASTVAL() function in MariaDB databases

The LASTVAL() function in MariaDB is used to retrieve the last generated value from a sequence. This function is used in conjunction with a sequence object. LASTVAL() can only be used after the sequence has been accessed with the NEXTVAL() function in the same session.
ISNULLLAST_INSERT_IDLAST_VALUELASTVALLCASELEASTLEFT

Further MySQL und MariaDB SQL Sequence functions

NEXTVALNEXTVAL(Sequence)
More about NEXTVAL Function

SETVALSETVAL(Sequence, Value [, Reserved] [, Cycle])
More about SETVAL Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the LASTVAL SQL function: mariadb.com
Updated: 2025-11-04ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedin©2020-2025 Heino Cunze-Fischer