简体   繁体   中英

Joins in SQL for retriving data from two tables

There are two tables A and B. You are retreiving data from both tables where all rows from B table and only matching rows from A table should be displayed. Which of the following types of joins will you apply between A and B tables?

- Inner join
- Left outer join
- Right outer join
- Self join

Use left outer hoin or right outer join.

For example, the following satisfy your requirement.

select * from tableB
Left outer join tableA
on tableB.ID= tableA.ID

Or

select * from tableA
Right outer join tableB
on tableA.ID= tableB.ID

Better way to understand:

替代文字

Easy, I would go with (B).

SELECT * FROM B x
LEFT JOIN A y
  on x.someColName = y.someColname

EDIT: can also use Right join

SELECT * FROM A x
RIGHT OUTER JOIN B y
  on x.someColName = y.someColname

这看起来像是家庭作业,但它已经足够简单了,我只会说你要求B LEFT JOIN A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM