简体   繁体   English

加入SQL以从两个表中检索数据

[英]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. 有两个表A和B.您正在从两个表中检索数据,其中应显示B表中的所有行以及仅显示A表中的匹配行。 Which of the following types of joins will you apply between A and B tables? 您将在A和B表之间应用以下哪种类型的连接?

- 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). 很容易,我会去(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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