Last Updated: 29 June, 2023
There is no built-in LAST() function in MySQL. However, we can achieve similar functionality by combining other functions or techniques.
If we want to retrieve the last row from a table based on a certain order, we can use the ORDER BY clause in conjunction with LIMIT 1.
For example:
SELECT * FROM your_table ORDER BY column_name DESC LIMIT 1;
In the above query, replace your_table with the name of the table and column_name with the actual column we want to order by. The DESC keyword specifies descending order, so the highest values will be returned. LIMIT 1 ensures that only the first row is retrieved.
If we want to retrieve the last inserted row in a table with an auto-increment primary key, we can use the LAST_INSERT_ID() function combined with a subquery.
SELECT * FROM your_table WHERE primary_key_column = (SELECT LAST_INSERT_ID());
Replace your_table with the name of the table and primary_key_column with the actual column name of our primary key.
That's all, guys. I hope this MySQL article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com