Help with an SQL query

I need help constructing a query.

My table has two columns: [Name] and [Friend]. The [Friend] column has a list of friends associated with each person in [Name]. An example:

Name         Friend
Joe            Bob
Joe            Steve
Bill             Doug
Bill             Steve
Steve        Adam

So a person can have multiple instance in both the [name] and [friend] columns. What I want to do is have a query that outputs all the people in [friend] who are not already in [name]. The idea is that for each person in [name] i have a list of friends. Sometimes those friends exist in [name] and sometimes they don't.

Oh, I'm running MySQL.

Thanks a log
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
here we go:
select t.Friend
from yourtable t
where not exists ( select null from yourtable i where = t.friend )

Open in new window

buktotruthAuthor Commented:
Tried as you described with actual column names:

select t.friendname from friendlist t where not exists ( select null from friendlist i where i.username = t.friendname )

got this error:
You have an error in your SQL syntax near 'exists ( select null from friendlist i where i.username = t.friendname )' at line 1
Guy Hengel [angelIII / a3]Billing EngineerCommented:
what version of MySQL are you using?
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

buktotruthAuthor Commented:
Good question. I'm running off of a remote server, and i'm not entirely sure how to check. I ran phpinfo() and found that the MySQL API version is 5.0.22. Does that help? If not, how do I figure out the version #?

select t.friendname from friendlist t
left outer join  friendlist i
on i.username = t.friendname
where i.username is null
try this one

select a.frnd from friend a
where  a.frnd not in (select distinct name from friend b)
buktotruthAuthor Commented:
ursnagel->same error as i was getting in the first place

appari-> that seems to work but it is VERY slow. My table has ~81,000 entries and when i run the command it just hangs. I tried looking at just the first 1500 entries to see how long it would take, and even that took about a minute. Is there any faster way to do this?
Guy Hengel [angelIII / a3]Billing EngineerCommented:
do you have an index on the table friendlist field username?
select t.Friendname
from friendlist as t
where not exists ( select null from friendlist as i where i.username = t.friendname )

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
buktotruthAuthor Commented:
Good point...building the index'll take a while. Will let you know if that speeds things up.

...thanks for the suggestion.
buktotruthAuthor Commented:
Thanks for all the help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.