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:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old May 3rd, 2008, 08:26 PM
Invalid02 Invalid02 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 213 Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 2 h 8 m 21 sec
Reputation Power: 11
Help with a query

Hey,

I'm having trouble getting my head around a query. I have the following tables:

users:
[username] | [name]

friends:
[username] | [friend_username]

Say a user has three friends:

users:
Will | William Brown
James | James Smith
Nathan | Nathan Bennett
Chris | Chris Carter
George | ...
Graham | ...
Edward | ...

friends:
Will | George
Geaorge | Will
Will | Graham
Graham | Will
Will | Edward
Edward | Will
Nathan | Chris
Chris | Nathan

I need a query that will return all the usernames/names that Will is not friends with - so in the above example it would return James/James Smith and Chris/Chris Carter (even though James is not in the friends table since he has no friends).

Thanks for any help.

Last edited by Invalid02 : May 3rd, 2008 at 09:18 PM.

Reply With Quote
  #2  
Old May 3rd, 2008, 09:03 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,727 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 19 h 22 m 42 sec
Reputation Power: 848
how are you storing the friends relationship? if i find a row with Will | George will i also find a row with George | Will? if not, why not?

does "direction" matter?
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old May 3rd, 2008, 09:18 PM
Invalid02 Invalid02 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 213 Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 2 h 8 m 21 sec
Reputation Power: 11
Quote:
Originally Posted by r937
how are you storing the friends relationship? if i find a row with Will | George will i also find a row with George | Will? if not, why not?

does "direction" matter?


Sorry that's my mistake - there is a row for George | Will as well as Will | Gearoge - I've updated me previous post.

What do you mean by direction?

Thanks

Reply With Quote
  #4  
Old May 3rd, 2008, 10:11 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,727 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 19 h 22 m 42 sec
Reputation Power: 848
Quote:
Originally Posted by Invalid02
What do you mean by direction?
if there were only one row, this would imply a direction -- George is a friend of Will

but since there are two rows, there is no direction, or, more correctly, there are both directions -- George is a friend of Will, and Will is a friend of George

Quote:
Originally Posted by Invalid02
I need a query that will return all the usernames/names that Will is not friends with

Code:
SELECT 
  FROM users
LEFT OUTER
  JOIN friends
    ON users.name IN ( friends.username, friends.friend_username)
 WHERE users.name = 'Will'
   AND friends.username IS NULL

Reply With Quote
  #5  
Old May 3rd, 2008, 10:30 PM
Invalid02 Invalid02 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 213 Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 2 h 8 m 21 sec
Reputation Power: 11
Quote:
Originally Posted by r937
Code:
SELECT 
  FROM users
LEFT OUTER
  JOIN friends
    ON users.name IN ( friends.username, friends.friend_username)
 WHERE users.name = 'Will'
   AND friends.username IS NULL


This doesn't seem to return me any rows - I'm confused with the lines:

ON users.name IN ( friends.username, friends.friend_username)
WHERE users.name = 'Will'

There will never be a users.name in either friends.username of friends.friend_name and users.name would be William Brown not Will. Did you perhaps mean:

Code:
SELECT users.username, users.name
  FROM users
LEFT OUTER
  JOIN friends
    ON users.username IN ( friends.username, friends.friend_username)
 WHERE users.username = 'Will'
   AND friends.username IS NULL


Although, this still doesn't return me any rows...

Thanks for your time.

Reply With Quote
  #6  
Old May 3rd, 2008, 10:41 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,727 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 19 h 22 m 42 sec
Reputation Power: 848
aaak!! what i should have said is
Code:
SELECT 
  FROM users
LEFT OUTER
  JOIN friends
    ON users.username  IN ( friends.username, friends.friend_username)
 WHERE users.username  = 'Will'
   AND friends.username IS NULL

Last edited by r937 : May 3rd, 2008 at 10:44 PM.

Reply With Quote
  #7  
Old May 3rd, 2008, 11:02 PM
Invalid02 Invalid02 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 213 Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 2 h 8 m 21 sec
Reputation Power: 11
Quote:
Originally Posted by r937
aaak!! what i should have said is
Code:
SELECT 
  FROM users
LEFT OUTER
  JOIN friends
    ON users.username  IN ( friends.username, friends.friend_username)
 WHERE users.username  = 'Will'
   AND friends.username IS NULL


I'm still having problems I'm afraid. My complete query looks like this:

Code:
SELECT users.username 
FROM users 
LEFT OUTER JOIN friends 
ON users.username IN ( friends.username, friends.friend_username ) 
WHERE users.username = 'Will' AND friends.username IS NULL


Could the problem be that my friends.username field, when created, was set to NOT NULL?

Thanks again.

Reply With Quote
  #8  
Old May 4th, 2008, 07:11 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,727 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 19 h 22 m 42 sec
Reputation Power: 848
oh man, i'm sorry, i really messed up last night

i tested this, so i know it works:
Code:
SELECT users.*
  FROM users 
LEFT OUTER
  JOIN friends
    ON friends.username = users.username AND friends.friend_username = 'Will' 
    OR friends.username = 'Will' AND friends.friend_username = users.username 
 WHERE users.username <> 'Will'
   AND friends.username IS NULL
there are two lines in the ON clause, you don't really need them both because your friendships are bidirectional and you store both rows

Reply With Quote
  #9  
Old May 4th, 2008, 08:41 AM
Invalid02 Invalid02 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 213 Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level)Invalid02 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 2 h 8 m 21 sec
Reputation Power: 11
Quote:
Originally Posted by r937
oh man, i'm sorry, i really messed up last night

i tested this, so i know it works:
Code:
SELECT users.*
  FROM users 
LEFT OUTER
  JOIN friends
    ON friends.username = users.username AND friends.friend_username = 'Will' 
    OR friends.username = 'Will' AND friends.friend_username = users.username 
 WHERE users.username <> 'Will'
   AND friends.username IS NULL
there are two lines in the ON clause, you don't really need them both because your friendships are bidirectional and you store both rows


Hehe, this works perfectly now - thank you very much for your persistence!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Help with a query


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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