Mysql limit usage

Source: Internet
Author: User
When a query statement is used, the first few or a few rows of data are often returned. What should I 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 first 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:
Mysql> SELECT * FROM table LIMIT 5; // retrieve the first five record rows

// In other words, LIMIT n is equivalent to LIMIT 0, n.

 

 

Instance:

Mysql> use sppeivan;
Database changed
Mysql> select * from employee;// Return all results
+ ------------ + -------- + ------------- +
| Username | password | gender | cellphone |
+ ------------ + -------- + ------------- +
| Ewrew | erewr | man | 13618889076 |
| Ewrwerw32r | 324324 | man | 13618889076 |
| Grrrrr | r44343 | woman | 1, 13618889076 |
| Pansong | erewrwe | man | 13618889076 |
| Ret43t43t | 43t43 | man | 13618889076 |
| Ret44t4 | r4tgr | woman | 13618889076 |
| Tttt | 4et | woman | 13618889076 |
| Wr34r | retre | man | 1, 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 syntax
Corresponds to your MySQL Server version for the right s
Line 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 | 1, 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 syntax
Corresponds to your MySQL server version for the right s
T 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 | 1, 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 | 1, 13618889076 |
| Pansong | erewrwe | man | 13618889076 |
| Ret43t43t | 43t43 | man | 13618889076 |
| Ret44t4 | r4tgr | woman | 13618889076 |
| Tttt | 4et | woman | 13618889076 |
| Wr34r | retre | man | 1, 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)

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: 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.