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 > Friends blocking list

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-12-10, 07:32
DavyBoy DavyBoy is offline
Registered User
 
Join Date: Aug 2010
Posts: 3
Friends blocking list

Hi,

I was reading DarkZlayers post about storing friends and my problem is kind of the opposite.

I have two tables, one standard users table with each user having a unique numerical ID and the other of blocked users, the first column being the ID of the user and the second being the ID of the user who has been blocked by that user.

So, let's say Dave wants to block Mary: the first column would be Dave's ID and the second would be Mary's ID. Dave should never see Mary in his list again, but Mary should be available for other others (unless they block her). At the same time, Mary should never see Dave again even though she's not performed any kind of blocking action.

I'm having real problems with the query to select users who are not blocked. I know I need to do two additional joins within the blocked users table but nothing I'm doing seems to work.

So for example: Dave's user id is 100 and he wants to view all users who aren't himself as well as those he hasn't blocked and those who haven't blocked him.

The query below doesn't work, but should give a list of all users Dave has blocked and who have blocked him:

select users.userid, firstname
from users
left join blocked_users a on (users.userid=a.blockeduserid and a.userid=100)
left join blocked_users b on (users.userid=b.userid and a.blockeduserid=100)


However, I want to do the *opposite* of this and I can't work out how. Although I'd also like to make the above query work too, though that's less important.

Any ideas?
Reply With Quote
  #2 (permalink)  
Old 08-12-10, 07:43
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
try this --
Code:
SELECT users.userid
     , users.firstname 
  FROM users
LEFT OUTER
  JOIN blocked_users a 
    ON a.userid = 100 
   AND a.blockeduserid = users.userid
LEFT OUTER
  JOIN blocked_users b 
    ON b.blockeduserid = 100
   AND b.userid = users.userid
 WHERE a.blockeduserid IS NULL
   AND b.blockeduserid IS NULL
alternatively, try this --
Code:
SELECT userid
     , firstname 
  FROM users
 WHERE NOT EXISTS
       ( SELECT 'uhoh'
           FROM blocked_users 
          WHERE blocked_users.userid = 100
            AND blocked_users.blockeduserid = users.userid 
             OR blocked_users.blockeduserid = 100
            AND blocked_users.userid = users.userid )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 08-12-10 at 07:47.
Reply With Quote
  #3 (permalink)  
Old 08-12-10, 20:59
DavyBoy DavyBoy is offline
Registered User
 
Join Date: Aug 2010
Posts: 3
Brilliant!

Sorry for my delay in responding, I didn't think anyone would get back to me so quickly! (I'm a first time poster)

I haven't tried the second solution yet, because the first one worked like a dream. I even noticed that I forgot to mention blocked users have a certain status and are not deleted if/when you unblock them, but your query allowed me to include that.

Thank you very much... I get to keep some hair now ;-)
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