SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Problem retrieving records from two tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

fuzzyjonclay
Starting Member

United Kingdom
3 Posts

Posted - 04/25/2012 :  06:17:10  Show Profile  Reply with Quote
Hi there,

I am new to SQL so please forgive me if this is a silly question :)

I need to retrieve records from one table where a certain condition exists in another table as follow:-

select address_number, address, town, county, postcode
from addresses
where exists (select * from organisations where status in ('FULL','BRAN'))
order by address_number

So, I want to retrieve all company records in the addresses table where the status of the company in the organisations table is equal to FULL or BRAN.

The above SQL is not working properly as 7082 records are retrieved whether I used the "where exists" or not.

Could anyone help me please! If it is useful, both the addresses and the organisations table share a key of address_number.

Many thanks,
Jon

SwePeso
Patron Saint of Lost Yaks

Sweden
29138 Posts

Posted - 04/25/2012 :  06:22:33  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Where is the relation between the outer select statement and the statement in the exists clause?



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

fuzzyjonclay
Starting Member

United Kingdom
3 Posts

Posted - 04/25/2012 :  06:23:40  Show Profile  Reply with Quote
Hi there,
Hmm, I'm not too sure exactly what you mean. I'm VERY new to SQL :)
Do I need to join the two table together somehow?
Cheers
Jon
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29138 Posts

Posted - 04/25/2012 :  06:24:07  Show Profile  Visit SwePeso's Homepage  Reply with Quote
select a.address_number, a.address, a.town, a.county, a.postcode
from dbo.addresses AS a
where exists (select * from dbo.organisations AS x where x.status in ('FULL','BRAN') and x.somecol = a.somecol)
order by a.address_number


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

fuzzyjonclay
Starting Member

United Kingdom
3 Posts

Posted - 04/25/2012 :  06:27:19  Show Profile  Reply with Quote
Genius.... thank you so much :)

I can now see what I did wrong too.

Best wishes
Jon
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000