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