如何在Sql中查询多个表的数据

时间:2020-12-27 00:50:37

I'm trying to get an extract of data using two tables in Sql. I have an AddressBook table and a companies table. The AddressBook table has a foreign key called companyid which is the primary key in the companies table. The companies table has a column called accountno. How do I lookup all the addresses on the AddressBook table and find the accountno in the companies table using the companyId?

我正在尝试使用Sql中的两个表来获取数据。我有一个AddressBook表和一个公司表。 AddressBook表有一个名为companyid的外键,它是companies表中的主键。公司表有一个名为accountno的列。如何查找AddressBook表中的所有地址,并使用companyId在companies表中找到accountno?

Please let me know if you need any more info

如果您需要更多信息,请告诉我

2 个解决方案

#1


4  

Use the JOIN, i think you want left join. With left join you fetch the companies even if they dont have an adress, but i see you have an inner join tag so i will include that.

使用JOIN,我想你想要离开加入。使用左连接即使他们没有地址也可以获取公司,但我看到你有一个内部连接标记,所以我将包含它。

left join:

SELECT * FROM companies LEFT JOIN adressbook ON adressbook.companyid = companies.id

inner join:

SELECT * FROM companies INNER JOIN adressbook ON adressbook.companyid = companies.id

#2


0  

select * 
from companies
inner join adressbook on adressbook.companyid = companies.id

if i read it correctly this is what you are looking for

如果我读得正确,这就是你要找的东西

#1


4  

Use the JOIN, i think you want left join. With left join you fetch the companies even if they dont have an adress, but i see you have an inner join tag so i will include that.

使用JOIN,我想你想要离开加入。使用左连接即使他们没有地址也可以获取公司,但我看到你有一个内部连接标记,所以我将包含它。

left join:

SELECT * FROM companies LEFT JOIN adressbook ON adressbook.companyid = companies.id

inner join:

SELECT * FROM companies INNER JOIN adressbook ON adressbook.companyid = companies.id

#2


0  

select * 
from companies
inner join adressbook on adressbook.companyid = companies.id

if i read it correctly this is what you are looking for

如果我读得正确,这就是你要找的东西