MySQL: select-union (Federated query) usage precautions

Source: Internet
Author: User

1. Requirements:

The number of substitute teachers in class 0115 was obtained, the results were sorted by ascending order, and 0228 classes were obtained, and the results were sorted in descending order. (1) first check the original class 0115 and class 0228 All substitute days, as follows: (2) Use the Union keyword, as follows: (3) Change the record of Han Xin in php0115 class is days =15, as follows: (4) When we go back to using the Union combination query, the problem will occur, as follows:

Attention:

If the result of the Union has duplicate records, then the duplicates are eliminated, similar to the distinct operation. How to solve this problem? You can use the union option all to achieve the goal.
(select T_name,days from Teacher_class where c_name= 'php0115' ORDER by days limit)Union all (Select T_name,days from Teacher_class where c_name= 'php0228' ORDER BY days desc LIMIT ); 2. Note: Modify the above SQL statement as:
(select T_name,days from Teacher_class where c_name= 'php0115' ORDER by days)UnionAll ( C9>select t_name,days from Teacher_class where c_name= 'php0228' order by days desc );
With the above limit removed, the results of the query are not displayed in the desired order : Sort by: sort the result of the child statement: (1). Wrap the child statement inside the parentheses. (2). The order by of the child statement takes effect only when order by IS in conjunction with limit. The reasons are: when the union is making a child statement, the order by of the limit clause is optimized (ignored)

MySQL: select-union (Federated query) usage precautions

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.