|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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? |
|
#3
|
|||
|
|||
|
Quote:
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 |
|
#4
|
||||
|
||||
|
Quote:
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:
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
|
|
#5
|
|||
|
|||
|
Quote:
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. |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
|||
|
|||
|
Quote:
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. |
|
#8
|
||||
|
||||
|
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
|
|
#9
|
|||
|
|||
|
Quote:
Hehe, this works perfectly now - thank you very much for your persistence! |
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > Help with a query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|