Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Last Entry in a MySQL table

Tags:

php

mysql

I'm basically trying to make a "goal" bar. The goal is determined by getting the last entry I made in a MySQL table. I want to get the ID of the last entry therefore.

How do I get the last entry in the table and then get the id from that last entry?

(Using PHP)

like image 228
ComputerLocus Avatar asked May 08 '12 17:05

ComputerLocus


People also ask

How can I see last inserted record in MySQL?

If you are AUTO_INCREMENT with column, then you can use last_insert_id() method. This method gets the ID of the last inserted record in MySQL.

How do you find the last value in a table?

We could use LAST_VALUE() in SQL Server to find the last value from any table. LAST_VALUE() function used in SQL server is a type of window function that results the last value in an ordered partition of the given data set.

How do I get the first and last record of a table in SQL?

To get the first and last record, use UNION. LIMIT is also used to get the number of records you want.


1 Answers

To get the greatest id:

SELECT MAX(id) FROM mytable

Then to get the row:

SELECT * FROM mytable WHERE id = ???

Or, you could do it all in one query:

SELECT * FROM mytable ORDER BY id DESC LIMIT 1
like image 155
Eric Petroelje Avatar answered Sep 28 '22 07:09

Eric Petroelje