Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

9-oracle_union and union all

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Union is a union operation on the result set, which requires that the two collections have the same fields and types.

Union: joins two result sets, excluding duplicate rows, and sorts the default rules

Union all: joins two result sets, including duplicate rows, without sorting

Union: we did two queries on the same table, and no duplicate 2 items appeared in the query results:

Select user_no, dept_code, sales_amt

From t_sales

Union

Select user_no, dept_code, sales_amt

From t_sales

Which statement is sorted by default rule? Is sorted in ascending order in the order in which we query the fields. The above is sorted by user_no,dept_code,sales_amt. To prove what we said, we change the order of the query fields and query by sales_amt,user_no,dept_code:

Select sales_amt, user_no, dept_code

From t_sales

Union

Select sales_amt, user_no, dept_code

From t_sales

The results are clearly sorted in ascending order in what we call the query fields.

Union all: merge the collections of 2 queries without dealing with duplicate rows:

Select user_no, dept_code, sales_amt

From t_sales

Union all

Select user_no, dept_code, sales_amt

From t_sales

We queried the table twice, so there were two records for each record. And the resulting dataset is out of order. In fact, this is our superficial phenomenon, and there is also an order in the returned result set: first return the results of the first query, the order of return is in the order of record insertion, and then return the result set of the second query. The order of return is also in the order of record insertion. This can be verified by the following two scripts:

Select user_no, dept_code, sales_amt

From t_sales a

Where a.sales_amt 3000

The first query returns three blue records.

Select user_no, dept_code, sales_amt

From t_sales a

Where a.sales_amt > 3000

Union all

Select user_no, dept_code, sales_amt

From t_sales a

Where a.sales_amt

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report