Home  >  Article  >  Database  >  How to use union in oracle

How to use union in oracle

WBOY
WBOYOriginal
2022-06-17 17:31:584970browse

In Oracle, union is used to combine the results of two SQL statements and exclude duplicate data. The field types of the two select statements match and the number of fields must be the same. The syntax is " select column,...from table1 union select column,...from table2".

How to use union in oracle

The operating environment of this tutorial: Windows 10 system, Oracle version 12c, Dell G3 computer.

How to use union in oracle

The purpose of the union command is to combine the results of two SQL statements so that you can view the query results you want. Duplicate records will be excluded.

For example:

SELECT Date FROM Store_Information
UNION
SELECT Date FROM Internet_Sales

Note: In union usage, the field types of the two select statements match, and the number of fields must be the same. As in the above example, in the actual software development process, it will When encountering more complex situations, please see the following example for details

How to use union in oracle

This sentence means to query the union of two SQL statements. The query condition is to look at the XMDA table Does the FL_ID match the FL_ID value in the main table FLDA (that is, it exists).

UNION will filter out duplicate records after table linking, so the result set generated will be correct after table linking Perform sorting operations, delete duplicate records and return the results.

You will encounter UNION ALL in the query. Its usage is the same as union, except that union has the distinct function. It will remove duplicate records from the two tables, but union all will not, so from In terms of efficiency, union all will be higher, but it is not used very much in practice.

The header will use the fields of the first connection block. . . . . . . . . .

UNION ALL simply combines the two results and returns them. In this way, if there is duplicate data in the two result sets returned, the returned result set will contain duplicate data.

In terms of efficiency, UNION ALL is much faster than UNION, so if you can confirm that the two combined result sets do not contain duplicate data, then use UNION ALL, as follows:

Recommended tutorial: "Oracle Video Tutorial"

The above is the detailed content of How to use union in oracle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn