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)
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.
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.
To get the first and last record, use UNION. LIMIT is also used to get the number of records you want.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With