union order by
时间: 2023-08-05 08:37:56 浏览: 47
"Union" is used to combine the results of two or more SELECT statements into a single result set. "Order by" is used to sort the result set by one or more columns in ascending or descending order.
When you use "Union" and "Order by" together, the "Order by" clause applies to the final result set after the union operation has been performed. This means that the entire result set will be sorted based on the column(s) specified in the "Order by" clause, regardless of which SELECT statement those columns came from.
For example, consider the following SQL query:
```
SELECT name, age FROM table1
UNION
SELECT name, age FROM table2
ORDER BY age DESC;
```
This query will combine the results of two SELECT statements (one from "table1" and one from "table2") and sort them by age in descending order. The final result set will contain all the distinct rows from both tables sorted by age.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)