Question

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?

Please let me know if you need any more info

Was it helpful?

Solution

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.

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

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top