How to query the last record through MySQL

Source: Internet
Author: User
MySQL query last record is seldom used in PHP, this article will explain its related operations.

The first thing to determine is what is the last one.
Is the most recent edit time for the last one, or the last one that has the largest number of fields.
For example, the most time is the last one, then the matching criteria of the data are filtered out, then sorted by time, and then take a piece of information.
SQL is as follows:

Select a, from table where a> ' some time ' order by a DESC limit 11

(A is time in SQL above).

Use Max (time) to query the party!!

Select Oid,status,max (time) time from table name GROUP by Oid,max (time), select * from TB where id = (SELECT max (ID) from TB); 12

MySQL Group takes the latest record (whole record)

MySQL takes the latest record after grouping, the following two methods. One is to first filter out the maximum and latest time in the even table query. One is to sort first, then in a sub-group query (default first), which is the latest data

SELECT * from T_assistant_article as a, (select Max (base_id) as base_id, Max (Create_time) as Create_time from T_assistant_ Article as B Group by base_id) as B where a.base_id=b.base_id and a.create_time = b.create_time  Select Base_id,max (cr Eate_time), Max (article_id) as article_id from T_assistant_article as B group by base_id   SELECT * FROM (SELECT * FROM T_assistant_article ORDER BY create_time Desc) as a group by base_id  12345

MySQL queries the first few lines of the row to the number of rows of records query the last row and row of records query previous rows and the following rows of records
1, query the first line of records:

Select   *   from   table  limit   11

2, query the nth line to the M record

SELECT * FROM table1  limit n-1,m-n; SELECT * FROM Table LIMIT 5, 10; Returns the record of rows 6th through 15th select * FROM Employee LIMIT 3, 1; Return to line 4th 123

3, query the first n rows of records

SELECT * FROM table1 limit 0,n; or select * FROM table1 limit n;123

4, after the query n rows of records

SELECT * FROM table1 ORDER BY id desc dlimit n;//reverse order, FETCH first n row ID as self-increment 1

5, query a record ($id) the next record

SELECT * FROM table1 where id> $id  ORDER by ID ASC Dlimit 11

6. Check the previous record of a record ($id)

SELECT * FROM table1 where id< $id  order BY id desc dlimit 1

This article on the MySQL query last record to make a detailed explanation, more learning materials clear focus on the PHP Chinese network can be viewed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.