MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 23rd, 2004, 04:46 AM
dast's Avatar
dast dast is offline
web muppet
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: blackpool, uk
Posts: 47 dast User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 19 m 29 sec
Reputation Power: 5
Question querying with multiple ids

Im pretty new to SQL and any help would be greatly appreciated

I have three tables, a property table, facility table and propertyFacilities table.

propertyFacilities table
--------------
facility_id
property_id

facility table
--------------
facility_id
facility_name

property_table
--------------
property_id
property_name
etc etc

A property can have quite a few facilities. In my statement i need to return the property which has all of the facilities that have been selected.

How can I do this?
Dave

Last edited by dast : February 23rd, 2004 at 05:03 AM.

Reply With Quote
  #2  
Old February 23rd, 2004, 06:39 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,921 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 58 m 55 sec
Reputation Power: 1022
Code:
select P.property_id
     , P.property_name
  from propertyFacilities PF
inner
  join property_table P
    on PF.property_id = P.property_id
group
    by P.property_id
     , P.property_name
having count(*) = 
       ( select count(*)
           from facility )    
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old February 23rd, 2004, 07:55 AM
dast's Avatar
dast dast is offline
web muppet
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: blackpool, uk
Posts: 47 dast User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 19 m 29 sec
Reputation Power: 5
thankyou very much for your reply

but whereabouts in the SQL would i pass through the id's? i have about 20 facility checkboxes on the previous page so based on the ones checked i need to find the property that matches.

Reply With Quote
  #4  
Old February 23rd, 2004, 08:13 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,921 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 58 m 55 sec
Reputation Power: 1022
finding the facilites matched, which may be fewer than all of them, is a slightly different query from finding properties with all facilities
Code:
select P.property_id
     , P.property_name
  from propertyFacilities PF
inner
  join property_table P
    on PF.property_id = P.property_id
 where PF.facility_id 
       in ( 3, 7, 14, 52 )  -- list the facility ids
group
    by P.property_id
     , P.property_name
having count(*) = 4         -- the number listed

Reply With Quote
  #5  
Old February 23rd, 2004, 08:17 AM
dast's Avatar
dast dast is offline
web muppet
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: blackpool, uk
Posts: 47 dast User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 19 m 29 sec
Reputation Power: 5
cheers mate - just waiting for SQL enterprise manager to install, i'll let you know how i get on!!

Reply With Quote
  #6  
Old February 23rd, 2004, 11:29 AM
dast's Avatar
dast dast is offline
web muppet
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: blackpool, uk
Posts: 47 dast User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 19 m 29 sec
Reputation Power: 5
works like a dream, thanks a lot mate

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > querying with multiple ids


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT