Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Latest Entry from Database

Tags:

date

sql

php

select

How can I get the latest entry by the latest DATE field from a MySQL database using PHP?

The rows will not be in order of date, so I can't just take the first or last row.

like image 657
user29772 Avatar asked Mar 10 '09 16:03

user29772


People also ask

How do I find the latest record in a database?

To get the last record, the following is the query. mysql> select *from getLastRecord ORDER BY id DESC LIMIT 1; The following is the output. The above output shows that we have fetched the last record, with Id 4 and Name Carol.

How do I get latest entries in SQL?

Here is the syntax that we can use to get the latest date records in SQL Server. Select column_name, .. From table_name Order By date_column Desc; Now, let's use the given syntax to select the last 10 records from our sample table.

How do I get last 10 rows in SQL?

mysql> SELECT * FROM ( -> SELECT * FROM Last10RecordsDemo ORDER BY id DESC LIMIT 10 -> )Var1 -> -> ORDER BY id ASC; The following is the output that displays the last 10 records. We can match both records with the help of the SELECT statement.


1 Answers

You want the ORDER BY clause, and perhaps the LIMIT clause.

$query = 'SELECT * FROM `table` ORDER BY `date` DESC LIMIT 1';
like image 159
Peter Bailey Avatar answered Sep 28 '22 16:09

Peter Bailey