Query the last two data records of the mysql database.
There is a mysql database table. the result of querying the last two pieces of data is as follows:
This is the content in the original table:
Idname
1 ad
2 jk
3 tre
4 hgv
This is the last two pieces of data to be queried (for the latest inserted data ):
Statement: select * from demo1 order by id desc limit 0, 2;
Result: id name
4 hgv
3 tre
The limit statement is used in the preceding query. here we will explain this attribute:
When we use a query statement, we often need to return the first few or a few rows of data in the middle. what should we do at this time? Don't worry,MysqlThis feature has been provided for us.
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET
The LIMIT clause can be used to force the SELECT statement to return the specified number of records. LIMIT accepts one or two numeric parameters. The parameter must be an integer constant. If two parameters are specified, the first parameter specifies the offset of the first returned Record Row, and the second parameter specifies the maximum number of returned record rows. The OFFSET of the initial Record Row is 0 rather than 1. to be compatible with PostgreSQL, MySQL also supports syntax: LIMIT # OFFSET #.
Mysql> SELECT * FROM table LIMIT 5, 10; // retrieves records FROM 6 to 15 rows.
// To retrieve all record rows from an offset to the end of the record set, you can specify the second parameter-1:
Mysql> SELECT * FROM table LIMIT 95,-1; // retrieves 96-last Records.
// If only one parameter is specified, it indicates the maximum number of record rows returned: m
Ysql> SELECT * FROM table LIMIT 5; // retrieves the first five record rows
// In other words, LIMIT n is equivalent to LIMIT 0, n.