Use of union in Oracle

Source: Internet
Author: User

UNIONThe purpose of the directive is to have twoSQLThe result of the statement is merged together,You can view the results of the query you want.

For example:

SELECT Date from Store_information
UNION
SELECT Date from Internet_sales

Note: Unionin use,two xSelectThe field type of the statement matches,and have the same number of fields,as the above example,in the actual software development process,will encounter more complicated situations,Please see the following example for details

Select ' 1 ' as Type,fl_id,fl_code,fl_cname,flda. Fl_parentid from Flda
WHERE zt_id=2006030002
Union
Select ' 2 ' as Type,xm_id,xm_code, Xm_cname, fl_id from Xmda
where exists (SELECT * FROM (select fl_id from Flda where zt_id=2006030002) a where xmda.fl_id=a.fl_id)
Order by Type,fl_parentid, fl_id

The meaning of this sentence is to put twoSQLStatementUnionQuery out,the condition of the query is to seeXmdain the tablefl_idwhether and the main tableFldain thefl_idvalues Match,(Which is the existence).

UNIONduplicate records are filtered after the table link is made, so the resulting set of results is sorted after the table is connected, the duplicate records are deleted, and the results are returned.

in the query, you will encounterUNION All,It's usage andUnionsame,justUnioncontaindistinctthe function,it will remove the duplicate records from the two tables.,andUnion Allwon't,so in terms of efficiency, Union allIt'll be a little higher.,but it's not a lot to use in practice..

the header will use the field of the first connection block ....

andUNION AllSimply merge the two results and return them. Thus, if there are duplicate data in the two result sets returned, the returned result set will contain duplicate data.

in terms of efficiency,UNION Allto be more thanUNIONmuch faster, so if you can confirm that the combined two result sets do not contain duplicate data, then use theUNION All, as follows:

try to useUnion All, becauseUnionneed to sort, eliminate duplicate records, and be inefficient

Use of union in Oracle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.