sql joining tables bringing wrong data

Hi I have the below tables

TBLImage
Id      ImageID      CompanyID
1        1                  1
2        2                  1
3        3                  1


TBLEmail
Id      EmailID      EmailAddress        CompanyID
1         1                  test                          1
2         3                  test                          1
3         2                  test                          1

So I want to get the data that gives me EmailID, CompanyID, and ImageID for a given company

so I wrote this

select Email.EmailID, Email.CompanyID,  img.ImageID, Email.EmailAddress
from dbo.TBLEmail Email
 join TBLImage img 
on img.CompanyID = Email.CompanyID
Where Email.CompanyID = 1

Open in new window


but this gives me wrong data as it repeats

Please help

Thanks

R8VI
R8VIAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
it should get you 3 images for each of the 3 records on table 2 unless you put a DISTINCT

select  DISTINCT Email.EmailID, Email.CompanyID,  img.ImageID, Email.EmailAddress
from dbo.TBLEmail Email
 join TBLImage img
on img.CompanyID = Email.CompanyID
Where Email.CompanyID = 1
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You need something in addition to expand the join condition. I cannot get what the meaning of those ID columns is in each table, so have to assume they are either completely unrelated (just a record counter) or should match. For the latter:
select Email.EmailID, Email.CompanyID,  img.ImageID, Email.EmailAddress
from dbo.TBLEmail Email
 join TBLImage img 
on img.CompanyID = Email.CompanyID
and img.ID = Email.ID
Where Email.CompanyID = 1

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Information Technology SpecialistCommented:
From your example, what results are you getting and what results would you expect?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.