MySQL: Basic Order by & amp; Limit sub-clause

Source: Internet
Author: User

MySQL: Basic Order by & Limit sub-clause 1. using basic 'order by 'java Code # order stu by stu_score as the Order of descendant select * from stu order by stu_score desc; 2. using 'ORDER BY' to order multiple columnsJava Code # order table 'std' by stu_score # If two row's stu_score are the same, then use stu_name as secondary order restriction select * from stu order by stu_score desc, stu_name desc; 3. Using 'ORDER by 'together with 'limit' SQL code # Select the highest two stu_score select * from stu order by stu_score asc limit 2; 4. limit [offset], [N] offset: default 0; N: The number of items to be selected. <Limit is useful in paging> Comment: How can we fetch the stu_course for each student whose highest score belongs to this course? Answer: 1. first, order the table stu as the stu_name as primary order regulator. 2. second, set the stu_score as the secondary order regulator. 3. last, regard the result set as a virtual table and the execute the query based on this table. SQL code select * from (select * from stu order by stu_name asc, stu_score desc) as temp group by stu_name; Summary: 1) where expression To see if the expression Is true on each row then fatch this row. = ,! =, <>,>, <, <=,> = In, between and or, and, not 2) group by Is used for grouping, usually used together with statistic function. sum, count, max, min, avg 3) having expression If the expression is true when used in where clause, then it can be applied in having expression. but if the expression is based on virtual table (or result set), then it can only applied in having expression. while expression can only applied for real table that stored on disk. 4) order by Can be applied for order real table or result set as the order of [asc] or [desc]. multiple compare rules are enabled. 5) limit Can be applied for result set for paging or limit the shown item.

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.