A tutorial on how to sort and limit the number of results returned in Mysql _mysql

Source: Internet
Author: User

MySQL ORDER BY Query results
ORDER BY
The ORDER BY keyword in SQL syntax is used to sort query results.
Sorting is divided into ascending (ASC) and Descending (DESC), and the default is ascending when the sort method is not specified by using order.
Grammar:

SELECT column,... From Tb_name to Column1,column2,... DESC (ASC)

You must list the sorted field names after order by, which can be multiple fields.
To query the user table UID in descending order:

SELECT Uid,username from User order by UID DESC

The results of the query are as follows:

UID username
4   Xiao Wang
3   Jack
2   xiaoming
1   Admin

Example 2:

SELECT username from user order by RegDate DESC LIMIT 10

This example queries the user name of the newly registered 10-person user.

MySQL Limit Limited Query record number
MySQL LIMIT
The LIMIT keyword in MySQL is used to limit the maximum number of query records returned.
Grammar:

... LIMIT offset, rows

In the syntax, offset represents the offset (the cursor to the data record), and rows represents the maximum number of records that the query qualifies for return, which must be an integer.
Example:

SELECT username from user LIMIT 4,10

If the data in the user table has more than 14 records, the example returns a 第5-14条 record that matches the result (total 10), noting that the default offset is 0.
The offset parameter, if omitted, defaults to 0, that is, LIMIT 10 is equivalent to LIMIT 0,10 (returns the first 10 records that meet the query criteria).
Tips
Rows in LIMIT do not support the value-1 (all data from the current offset to the end of the table record), such as:

SELECT username from user LIMIT 9,-1

Running this SQL will result in parameter errors.
Small Tips
When you confirm that the query results have only one piece of data (such as checking user name password), you can add the limit 1 restrictions, when the system query to a data that stops searching without continuing to find the next record, which can effectively improve query efficiency.

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.