SunQuest
           MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

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:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old June 27th, 2000, 12:06 PM
strider3700 strider3700 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 8 strider3700 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok I have 3 tables, parks(id, name), activities(id,name) and contains(park_id, activity_id). The basic idea is parks contain activities. Now what I'm trying to so is get a list back of what parks contain specified activities. So what parks contain activities 1, 4 and 6... I can't seem to get the join correct once I'm looking for multiple activities. Any help would be appreciated.

Reply With Quote
  #2  
Old June 28th, 2000, 07:28 AM
Kyuzo Kyuzo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 1999
Location: Annapolis, Maryland US
Posts: 113 Kyuzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
> SELECT distinct(t1.name)
> FROM parks as t1, activities as t2, contains as t3
> WHERE t1.id=t3.park_id and t2.id=t3.activity_id
> AND t3.activity_id IN(1,4,6)

Reply With Quote
  #3  
Old June 28th, 2000, 07:33 AM
Kyuzo Kyuzo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 1999
Location: Annapolis, Maryland US
Posts: 113 Kyuzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Oops! Sorry, you might want to change the 't3.activity_id IN(1,4,6)' to 't3.activity_id=1 AND t3.activity_id=4...' etc to check for multiple activities

Reply With Quote
  #4  
Old June 28th, 2000, 12:54 PM
strider3700 strider3700 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 8 strider3700 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I've tried what you've suggested but it still doesn't find multiple activities. The structure of the contains table is given in the original post. So if park 2 contains activities 1,3,5 there are 3 records in the table 2,1 2,3 2,5 I can find that park 2 has activity 1 with both my original query and the one suggested, but neither is able to find that park 2 has activity 1 and activity 3. is this a problem with my tables or am I just getting the query wrong?

Reply With Quote
  #5  
Old June 28th, 2000, 04:12 PM
Kyuzo Kyuzo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 1999
Location: Annapolis, Maryland US
Posts: 113 Kyuzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
An alternate suggestion would be to rework the parks table to include a set() datatype....

> create table parks(
> id int not null auto_increment primary key,
> name varchar(25),
> activity_list set('1','2','3','4'...'n'));


Then you could use a query like
> select name from parks
> where find_in_set(1, activity_list)
> AND find_in_set(3, activity_list)
> AND find_in_set(6, activity_list)..etc;

You could probably just substitute the numbers for the actual activity name, i.e. 'tennis', 'softball'....whatever. I believe the set() datatype can hold up to 64 members - check the mysql manual

Reply With Quote
  #6  
Old June 28th, 2000, 04:55 PM
strider3700 strider3700 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 8 strider3700 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ok this is sick and hideous to do dynamically, but I figured I'd share the solution I came up with. Basically for X number of activities being searched for I do X self joins on the contains table. So to find all parks with activites 1,3,5 X=3 and the query is
SELECT distinct(t3.park_id) FROM contains AS t1, contains AS t2, contains AS t3 WHERE t1.activity_id =1 and t2.activity_id =3 and t3.activity_id =5;

Have to thank Kyuzo for all your help

if anyone has a better way to do this I'd love to hear it

jamie

Reply With Quote
  #7  
Old June 28th, 2000, 05:48 PM
strider3700 strider3700 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 8 strider3700 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ok this is sick and hideous to do dynamically, but I figured I'd share the solution I came up with. Basically for X number of activities being searched for I do X self joins on the contains table. So to find all parks with activites 1,3,5 X=3 and the query is
SELECT distinct(t3.park_id) FROM contains AS t1, contains AS t2, contains AS t3 WHERE t1.activity_id =1 and t2.activity_id =3 and t3.activity_id =5;

Have to thank Kyuzo for all your help

if anyone has a better way to do this I'd love to hear it

jamie

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > help with this join


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 6 hosted by Hostway