Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 Other Topics
 sql query

Author  Topic 

gorrumpus
Starting Member

1 Post

Posted - 2009-03-19 : 12:51:29
Any help here will be very much appreciated!
Here is my problem.

I have two tables - "Users" and "Users_linked"
The "users" table contains id's, names, address etc of a registered user to a site. The second table, "users_linked" is simply a table containing 3 fields "id" "user_id_1" and "user_id_2".

There are user id's in both the fields "user_id_1" and "user_id_2".
What i would like to do for example, is to find all the records in the "user" table where in the table "users_linked" the "user_id_1" or "user_id_2" fields equals "17" (17 being a user id from the "users" table).

The second thing i would like is the opposite.
Find all "users" where the user id "17" is not in any of the fields "user_id_1" and "user_id_2" in the table "users_linked".

As you may guess, its to allow users to have contacts/friends like a social networking site.

Any help would be much help! Thanks!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-15 : 17:57:02
SELECT *
FROM Users AS u
INNER JOIN Users_Linked AS ul ON 17 in (ul.user_id_1, ul.user_id_2)



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -