Oracle data association query

Source: Internet
Author: User

Source: Saidi net Author: alizze

Associations are frequently used in Oracle Data Queries. Flexible application associations can solve many practical problems. The following are some examples:

Create a table


      
       create table ab
       
(
ab_id number(5),
ab_name varchar2(30)
);

create table bb
(
bb_id number(5),
bb_name varchar2(30)
);

Insert data


      
       insert into ab(ab_id,ab_name) values(1,'1ab_name');
       
insert into ab(ab_id,ab_name) values(2,'2ab_name');
insert into ab(ab_id,ab_name) values(3,'3ab_name');

insert into bb(bb_id,bb_name) values(1,'1bb_name');
insert into bb(bb_id,bb_name) values(2,'2bb_name');
insert into bb(bb_id,bb_name) values(4,'4bb_name');
insert into bb(bb_id,bb_name) values(6,'6bb_name');

Equivalent Association (full association)


      
       select a.ab_id,a.ab_name,b.bb_name from ab a,
       
bb b
where a.ab_id=b.bb_id;

Returned results


      
       AB_ID AB_NAME BB_NAME
       
1 1ab_name 1bb_name
2 2ab_name 2bb_name

Returns all records that can be matched in two tables. Records that cannot be matched are not returned.

Left join


      
       
Select AB _id, AB _name, bb_name
       
From AB a left join BB B
On a. AB _id = B. bb_id;
Same
Select AB _id, AB _name, bb_name
From AB a, BB B
Where a. AB _id = B. bb_id (+ );

Returned results


      
       AB_ID AB_NAME BB_NAME
       
1 1ab_name 1bb_name
2 2ab_name 2bb_name
3 3ab_name

Return the left and right records of the table on the left. The right table fields with no corresponding records in the right table are blank.

Right join


      
       
Select AB _id, AB _name, bb_name
       
From AB a right join BB B
On a. AB _id = B. bb_id;
Same
Select AB _id, AB _name, bb_name
From AB a, BB B
Where a. AB _id (+) = B. bb_id;

Returned results:


      
       AB_ID AB_NAME BB_NAME
       
1 1ab_name 1bb_name
2 2ab_name 2bb_name
4bb_name
6bb_name

Return all records in the right table. The fields in the left table are blank on the rows of records that cannot be matched in the right table.

Completely external Association


      
       select ab_id,ab_name, bb_name 
       
from ab a full join bb b
on a.ab_id=b.bb_id;

Returned results


      
       AB_ID AB_NAME BB_NAME
       
1 1ab_name 1bb_name
2 2ab_name 2bb_name
3 3ab_name
6bb_name
4bb_name

Returns all the results of the two tables. If no corresponding record exists in the join condition, the column is blank.

The range of result sets returned by the full join operation is the smallest. The results returned by the full outer join operation have the largest range, and whether the left and right join operations are the same. The full join operation is the intersection of the left and right join operations.

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.