How to select the last 5 rows from my mysql

I wanted to know the sql command to extract the last 5 rows from my table? Below is my sql query. How will I do so that he can select the last 5 base rows in a non-process row?

$query = 
"SELECT lat, lng, DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS 
  datetime FROM markers1 WHERE 1";
+3
source share
5 answers

You need to order results and set a limit.

Assuming date-time is what you wanted to order:

$query = "
    SELECT 
        lat, 
        lng, 
        DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS datetime 
    FROM markers1 WHERE 1
    ORDER BY datetime DESC
    LIMIT 5
";

EDIT: To respond to OP's comment: “the result I get begins with row Row 50 for the first request, and follows 49,48,47,46. Is it possible that I can get this start on line 46,47, 48.49.50? "

PHP, . , mysql .

SQL-, :

$query = "
    SELECT 
        lat,
        lng,
        DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS datetime 
    FROM (
        SELECT 
            lat, 
            lng, 
            datetime
        FROM markers1 WHERE 1
        ORDER BY datetime DESC
        LIMIT 5
    ) AS tmp_markers
    ORDER BY datetime ASC
";

, . DATE_FORMAT , datetime.

+7

:

ORDER BY datetime DESC
LIMIT 5
+1

- , , , datetime ; , 5 5, , 5 datetime.

SELECT
    lat
    , lng
    , DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS datetime
    , @rownum:=@rownum+1 `RowNum`
FROM
    markers1
    , (SELECT @rownum:=0) `r`
WHERE 1
ORDER BY RowNum DESC
LIMIT 5

blog .

+1
source

It may be a very late answer, but it is good and simple.

select * from table_name order id desc limit 5

This query will return a set of the last 5 values ​​(last 5 rows) that you entered in the table

0
source

Check my request for the last 5 entries

SELECT * FROM notices where organizationid = $orgid ORDER BY `notices`.`id` DESC LIMIT 5

So, the most important is ORDER BY notices.id DESC LIMIT 5

0
source

All Articles