MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 11th, 2004, 07:58 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
MySQL joining same table

I've been stumped for a week now... and I'm pretty sure I've frustrated all my friends...

The easiest way to explain this problem is by example...
Basically I have a bridging table linking members and events... I want to find which events that two specific members attended together...

The table looks like this:
userid, eventid

Oracle has simple nesting query techniques, and things like union, join and intersect... However I'm struggling to find a way to do this with a MySQL query...

Any suggestions?

Reply With Quote
  #2  
Old March 12th, 2004, 12:18 AM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 8 m 57 sec
Reputation Power: 9
Send a message via ICQ to stumpy Send a message via MSN to stumpy
Would something like "GROUP BY eventid" help?
__________________
DevArticles Moderator
BlueSix - Web Development and Consulting

Reply With Quote
  #3  
Old March 12th, 2004, 09:58 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
I've tried various GROUP BY, but it still shows events that each member attended on their own... I'm looking specifically for only the ones they both attended...

I've tried LEFT JOINs and RIGHT JOINs... but the fact that its from the same table is confusing me...

I keep comparing it to Oracle, because this could have been done something similar:
Code:
SELECT eventid
FROM user_event
WHERE userid=1
AND eventid IN (SELECT eventid FROM user_event where usedid=2)


I can't figure out the MySQL equavelent

Reply With Quote
  #4  
Old March 12th, 2004, 10:05 AM
Spongy's Avatar
Spongy Spongy is offline
Alternately High
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Hilversum, Netherlands
Posts: 223 Spongy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 56 m 41 sec
Reputation Power: 5
Send a message via MSN to Spongy
I don't know much of SQL, only the basics, but isn't there just a simple way of doing this?
Code:
SELECT eventid
FROM user_event
WHERE userid=1
AND userid=2

Dunno for sure, but this is something I would try. I don't understand the JOINs
__________________
Work to live, don't live to work

Reply With Quote
  #5  
Old March 12th, 2004, 12:38 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Spongy,
how could one id be equal to two values? =)

I've tried using OR... (where id=1 or id=2)
However this returns all the events that both users attended, including ones that only one person went alone...

I need an intersect somehow, but can't figure out how MySQL handles this...

Reply With Quote
  #6  
Old March 12th, 2004, 01:18 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Hmmm, I guess you could do something like this:

Code:
select eventid, userid from user_event where eventid=1 AND userid in(1,2);


and figure it's a valid result only if the row count is greater than one.
__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
  #7  
Old March 12th, 2004, 01:26 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
dhouston, does that only work for one specific event?
how would I get all the events that the two people share?

[i'll try the actual query when i get home from work]

Reply With Quote
  #8  
Old March 12th, 2004, 01:35 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Scrap that. I was assuming you were checking for a given event. Try this:

Code:
mysql> describe user_event;
+---------+---------------------------+------+-----+---------+-------+
| Field   | Type                      | Null | Key | Default | Extra |
+---------+---------------------------+------+-----+---------+-------+
| userid  | int(10) unsigned zerofill | YES  |     | NULL    |       |
| eventid | int(10) unsigned zerofill | YES  |     | NULL    |       |
+---------+---------------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
 
mysql> select * from user_event;
+------------+------------+
| userid     | eventid    |
+------------+------------+
| 0000000003 | 0000000001 |
| 0000000002 | 0000000001 |
| 0000000001 | 0000000001 |
| 0000000001 | 0000000002 |
+------------+------------+
4 rows in set (0.00 sec)
 
mysql> select eventid, count(userid) as cnt from user_event where userid in(1,2) group by eventid;
+------------+-----+
| eventid    | cnt |
+------------+-----+
| 0000000001 |   2 |
| 0000000002 |   1 |
+------------+-----+
2 rows in set (0.00 sec)


You still have to verify that the row count for a given event is greater than one. Maybe this'll get you going in the right direction, at least.

Reply With Quote
  #9  
Old March 12th, 2004, 11:02 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Wow dhouston... that's awesome!
I never would have thought to have done it this way.
I see the light! =)

Curious, how come i can't use cnt in a where clause?
For example, where cnt=2... similarly, although i somewhat understand, I can't use where count(userid)=2
However, if i were to use as for a table, i'd have to refer to it as the designated name

Reply With Quote
  #10  
Old March 15th, 2004, 08:18 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
My guess is that you can't use cnt in the where clause because it can't be assembled until after the result set is in. SInce it's an aggregate function whose results are contingent upon parsing all the data otherwise selected for, it can't very well be used in the where clause before it's calculated. Not sure about that, but it makes sense to me.

Incidentally, if you're wanting to get really fancy with this, you can stick the user ids in an array, join the array with commas to stick in the IN clause, and then use sizeof() your array to verify that only events that all listed participants are signed up for will be displayed. This would allow for dynamic selection (without hard-coding numbers), a good thing for code maintainability.

Reply With Quote
  #11  
Old March 15th, 2004, 08:22 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
To clarify my last statement, it doesn't appear that you could do this in a where clause (based on your last question). But you could compare sizeof() to the count and only handle elegible records.

Reply With Quote
  #12  
Old March 17th, 2004, 12:47 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
I guess what you're saying about cnt being assembled after the result set is in makes sense... i suppose it could make a double-pass over the result set, but that would increase the load on the server for larger databases... for now i'm handling that much in my pgoramming language [if (cnt=3)]

Similarly, I already build my query dynamically, joining the ids I wish to search, and testing IF(cnt=sizeof(id_Array))... similar to what you've mentioned...

It would just be nice if MySQL would have handled all that for me and ONLY returned the values where cnt is 3... none-the-less, it works... a much appreciated query solution! =)

Reply With Quote
  #13  
Old March 17th, 2004, 01:07 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
There could be a way to do this that I simply don't know. I'm kind of a hack and slash query writer who comes up with things that work, even if they're not the optimal solution. Glad I could help out in this case.

Reply With Quote
  #14  
Old April 6th, 2005, 06:09 AM
camsoft camsoft is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 1 camsoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 23 sec
Reputation Power: 0
Smile

Quote:
Originally Posted by dhouston
My guess is that you can't use cnt in the where clause because it can't be assembled until after the result set is in. SInce it's an aggregate function whose results are contingent upon parsing all the data otherwise selected for, it can't very well be used in the where clause before it's calculated. Not sure about that, but it makes sense to me.

Incidentally, if you're wanting to get really fancy with this, you can stick the user ids in an array, join the array with commas to stick in the IN clause, and then use sizeof() your array to verify that only events that all listed participants are signed up for will be displayed. This would allow for dynamic selection (without hard-coding numbers), a good thing for code maintainability.


You can access expressions in the select by using the HAVING clause insted of WHERE.
HAVING evalulates after the SELECT part is parsed.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > MySQL joining same table


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