Home > Database > Oracle > body text

How to use union in oracle

下次还敢
Release: 2024-04-30 07:06:15
Original
781 people have browsed it

The usage of UNION in Oracle is to merge multiple query result sets with the same structure into a single result set. This operator removes duplicate records unless UNION ALL is used, which merges all records, including duplicates.

How to use union in oracle

UNION usage in Oracle

UNION is used in Oracle to combine multiple queries SQL operator that combines result sets into a single result set. It is used to merge tables or query results that have the same structure (column names and data types).

Syntax:

<code>SELECT ...
UNION
SELECT ...
UNION
...</code>
Copy after login

Usage:

  • Merge tables with the same structure:

    <code>SELECT * FROM table1
    UNION
    SELECT * FROM table2;</code>
    Copy after login
  • Merge different queries:

    <code>SELECT name, age FROM students
    UNION
    SELECT name, NULL AS age FROM teachers;</code>
    Copy after login

Note:

  • Only columns with the same number and data type can be merged.
  • UNION will delete duplicate records unless UNION ALL is used.
  • UNION ALL will combine all records, including duplicate records.

Example:

The following table contains two tables:

table1

##1John202Mary25
id name age

table2

##id34Using UNION, we can merge these two tables:
name job
Bob teacher
Alice student
<code>SELECT * FROM table1
UNION
SELECT id, name, NULL AS job FROM table2;</code>
Copy after login

Result:

id1234
name age job
John 20 null
Mary 25 null
Bob null teacher
Alice null student

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!