If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > JOIN problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-20-11, 13:10
merik merik is offline
Registered User
 
Join Date: Apr 2011
Posts: 32
JOIN problem

On a shared document system, I have a table which has three fields: pageid, editid, and userid. Each user may edit any page zero or more times.

I want to find all pages in which user 1 and user 2 have both edited, and get a list of all the revisions they made on these pages. The output should be sorted by page ID and look like this:

pageid editid userid
-----------------------
12 143 1
12 155 2
12 189 2
17 100 1
17 104 2
17 105 1
17 199 1
...

I tried this query but it is very slow, and it doesn't actually find the "mututal" pages:

SELECT editid FROM revision WHERE pageid IN (SELECT DISTINCT pageid FROM revision WHERE userid IN (1,2) ORDER BY pageid);

What is an efficient query?

Last edited by merik; 05-21-11 at 18:18. Reason: slow query
Reply With Quote
  #2 (permalink)  
Old 05-21-11, 10:31
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
I am not sure whether this is going to give the correct answer. The WHERE userid IN (1,2) is basically where userid = 1 OR userid = 2.

Try this:

SELECT rev_id FROM revision WHERE pageid IN (SELECT pageid FROM revision WHERE userid IN (1,2) GROUP BY pageid HAVING COUNT(*) = 2);
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 05-21-11, 18:25
merik merik is offline
Registered User
 
Join Date: Apr 2011
Posts: 32
Thank you for your reply. Your query doesn't list the common pages either.
Reply With Quote
  #4 (permalink)  
Old 05-22-11, 11:49
merik merik is offline
Registered User
 
Join Date: Apr 2011
Posts: 32
Never mind, I found the solution myself.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On