SQL statements for mysql to obtain the latest several records

Source: Internet
Author: User
The SQL statement for obtaining the latest 10 records is as follows: select * fromsome_tableorderbyiddesclimit0, 10SELECT * FROMtableLIMIT [offset,] rows | rowsOFFSEToffsetLIMIT clause can be used to force the SELECT statement to return the specified number. MySQLLIMIT accepts one or two numeric parameters. The parameter must be 1.

The SQL statement for obtaining the latest 10 records is as follows: select * fromsome_table order by id desc limit SELECT * FROMtableLIMIT [offset,] the rows | rowsOFFSEToffset LIMIT clause can be used to force the SELECT statement to return the specified number of records. MySQL LIMIT accepts one or two numeric parameters. The parameter must be 1.

The SQL statement for obtaining the latest 10 records is as follows:

Select * from some_table order by id desc limit 0, 10

 
 
  1. SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset

The LIMIT clause can be used to force the SELECT statement to return the specified number of records. MySQL 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 #.

 
 
  1. MySQL> SELECT * FROM table LIMIT 5,10;

Retrieve record lines 6-15

To retrieve all record rows from an offset to the end of the record set, you can specify the second parameter-1:

 
 
  1. MySQL> SELECT * FROM table LIMIT 95,-1;

Retrieve the 96-last record row.

If only one parameter is specified, it indicates the maximum number of record rows returned:

 
 
  1. MySQL> SELECT * FROM table LIMIT 5;

Retrieve the first five record rows. In other words, LIMIT n is equivalent to MySQL LIMIT 0, n.

Example:

Mysql> use sppeivan; Database changedmysql> select * from employee; // return all results + ------------ + -------- + ------------- + | username | password | gender | cellphone | + ------------ + -------- + ------------- + | ewrew | erewr | MAN | 13618889076 | ewrwerw32r | 324324 | MAN | 13618889076 | grrrrr | r44343 | WOMAN | 13618889076 | pansong | erewrwe | MAN | 13618889076 | ret43t43t | 43t43 | MAN | 13618889076 | ret44t4 | r4tgr | WOMAN | 13618889076 | tttt | 4et | WOMAN | 13618889076 | wr34r | retre | MAN | 13618889076 | xxx | safe | MAN | 13618889076 | hhh | ewrwer | MAN | 13618889076 | hg | 6574326543 | MAN | 13618889076 | ps | ewrwe | MAN | 13618889076 | zhu | werew | MAN | 13618889076 | + ------------ + ---------- + -------- + ------------- + 13 rows in set (0.01 sec) mysql> select * from employee limit 3 7; ERROR 1064 (42000 ): you have an error in your SQL syntaxcorresponds to your MySQL server version for the right sline 1 mysql> select * from employee limit 3, 7; // return 4-11 rows + ----------- + ---------- + -------- + ------------- + | username | password | gender | cellphone | + ----------- + ---------- + -------- + ------------- + | pansong | erewrwe | MAN | 13618889076 | ret43t43t | 43t43 | MAN | 13618889076 | ret44t4 | r4tgr | WOMAN | 13618889076 | tttt | 4et | WOMAN | 13618889076 | wr34r | retre | MAN | 13618889076 | xxx | safe | MAN | 13618889076 | hhh | ewrwer | MAN | 13618889076 | + ----------- + ---------- + -------- + ------------- + 7 rows in set (0.00 sec) mysql> select * from employee limit 3,-1; // ERROR syntax ERROR 1064 (42000 ): you have an error in your SQL syntaxcorresponds to your MySQL server version for the right st line 1 mysql> select * from employee limit 3,1; // return 4th rows + ---------- + -------- + ------------- + | username | password | gender | cellphone | + ---------- + -------- + ------------- + | pansong | erewrwe | MAN | 13618889076 | + ---------- + -------- + ------------- + 1 row in set (0.00 sec) mysql> select * from employee limit 3; // return the first three rows + ------------ + ---------- + -------- + ------------- + | username | password | gender | cellphone | + ------------ + ---------- + -------- + ------------- + | ewrew | erewr | MAN | 13618889076 | ewrwerw32r | 324324 | MAN | 13618889076 | grrrrr | r44343 | WOMAN | 13618889076 | + ------------ + ---------- + -------- + ----------- + 3 rows in set (0.00 sec) mysql> select * from employee limit 10; // return the first 10 rows + ------------ + ---------- + -------- + ------------- + | username | password | gender | cellphone | + ------------ + ---------- + -------- + ------------- + | ewrew | erewr | MAN | 13618889076 | ewrwerw32r | 324324 | MAN | 13618889076 | grrrrr | r44343 | WOMAN | 13618889076 | pansong | erewrwe | MAN | 13618889076 | ret43t43t | 43t43 | MAN | 13618889076 | ret44t4 | r4tgr | WOMAN | 13618889076 | tttt | 4et | WOMAN | 13618889076 | wr34r | retre | MAN | 13618889076 | xxx | safe | MAN | 13811588469 | hhh | ewrwer | MAN | 13618889076 | + ------------ + ---------- + -------- + ------------- + 10 rows in set (0.00 sec) mysql> select * from employee limit 9, 1; // return 10th rows + ---------- + -------- + ------------- + | username | password | gender | cellphone | + ---------- + -------- + ------------- + | hhh | ewrwer | MAN | 13816668468 | + ---------- + -------- + ------------- + 1 row in set (0.00 sec)

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: info-contact@alibabacloud.com 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.