Skip to content Skip to sidebar Skip to footer

Php Mysql Sort By Date (recent)

I have now my code is fetching data in mobile application from SQL as first added showing first, I need to set it as showing last added as first in my android application. I have a

Solution 1:

When you use the SELECT statement to query data from a table, the result set is not sorted in any orders. To sort the result set, you use the ORDER BY

like that :-

ORDERBY column1 [ASC|DESC]

in your code :-

$query="SELECT * FROM tbl_quotes
    LEFT JOIN tbl_category ON tbl_quotes.cat_id= tbl_category.cid 
    ORDER BY tbl_quotes.id DESC,date DESC LIMIT $limit";

if you column data type is not date or datetime us cast() like that :-

CAST(column_name AS DATETIME);
CAST(column_name AS DATE);

Solution 2:

Use order by for date as well.

ORDERBY tbl_quotes.id DESC, DATE(your_date_column) desc

Also see this documentation for more information

Post a Comment for "Php Mysql Sort By Date (recent)"