MYSQL LAST Function
Category : TECH Author : Ankur Rastogi Date : Sat Jun 04 2016 Views : 212

MYSQL LAST Function returns the last match, value, row or redord for a given condition. Last function is more useful when we have to get what is the last entry in the table for any given condition or situation.
Syntax fot MYSQL LAST Function is
SELECT LAST(column_name) FROM table_name WHERE [condition];
Let us understand better with an example.
Consider student_list table with details for all the students.
+-----+------------------+--------+-----------+
| sid | name | year | hostel_id |
+-----+------------------+--------+-----------+
| 1 | Ankur Rastogi | First | 4 |
| 2 | Amit Kumar | Second | 8 |
| 3 | Ram Kumar | Second | 8 |
| 4 | Shiv Saxena | First | 6 |
| 5 | Aman Aggarwal | Third | 3 |
| 6 | Nipun Gupta | Fourth | 2 |
| 7 | Arpit Gupta | Third | 1 |
| 8 | Shantanu Rastogi | Second | 3 |
| 9 | Ramesh Kumar | Third | 1 |
| 10 | Neelima Dhingra | Second | NULL |
| 11 | Anu Sibbal | Third | NULL |
+-----+------------------+--------+-----------+
Let us find the name of last student who entered in Third Year. MYSQL query for that will be
SELECT LAST(name) FROM student_list
WHERE year = 'Third';
+------------+
| name |
+------------+
| Anu Sibbal |
+------------+
FIRST function is not supported by some of the databases. So there we use work around for ex in MYSQL we can write the same query as
SELECT name FROM student_list
WHERE year = 'THIRD' ORDER BY sid DESC LIMIT 1;

MYSQL LAST Function returns the last match, value, row or redord for a given condition. Last function is more useful when we have to get what is the last entry in the table for any given condition or situation.
Syntax fot MYSQL LAST Function is
SELECT LAST(column_name) FROM table_name WHERE [condition];
Let us understand better with an example.
Consider student_list table with details for all the students.
+-----+------------------+--------+-----------+
| sid | name | year | hostel_id |
+-----+------------------+--------+-----------+
| 1 | Ankur Rastogi | First | 4 |
| 2 | Amit Kumar | Second | 8 |
| 3 | Ram Kumar | Second | 8 |
| 4 | Shiv Saxena | First | 6 |
| 5 | Aman Aggarwal | Third | 3 |
| 6 | Nipun Gupta | Fourth | 2 |
| 7 | Arpit Gupta | Third | 1 |
| 8 | Shantanu Rastogi | Second | 3 |
| 9 | Ramesh Kumar | Third | 1 |
| 10 | Neelima Dhingra | Second | NULL |
| 11 | Anu Sibbal | Third | NULL |
+-----+------------------+--------+-----------+
Let us find the name of last student who entered in Third Year. MYSQL query for that will be
SELECT LAST(name) FROM student_list
WHERE year = 'Third';
+------------+
| name |
+------------+
| Anu Sibbal |
+------------+
FIRST function is not supported by some of the databases. So there we use work around for ex in MYSQL we can write the same query as
SELECT name FROM student_list
WHERE year = 'THIRD' ORDER BY sid DESC LIMIT 1;
Disclaimer: The above content reflect author’s personal views and do not reflect the views of OYEWIKI. Neither OYEWIKI nor any person/organization acting on its behalf is liable to accept any legal liability/responsibility for any error/mislead in this information or any information available on the website. This website in no way accepts the responsibility for any loss, injury, damage, discomfort or inconvenience caused as a result of reliance on any information provided on this website.
If you want to add more comments to the article or you see any thing incorrect please write a comment below and we will surely get back to you.