views:

120

answers:

4

Hello StackOverflow Community,

I had a quick question on how to exclude data from an SQL database using an SQL statement. My situation is I have a user login to their profile page where they will be able to friend people. I want to display all users except themselves that are found in the SQL database.

Thanks for all the answers!

Rohan

+2  A: 

How about:

SELECT * FROM people WHERE person_id != $current_user_id
AJ
That should probably be person_id <> current_user_id
Andomar
Why? The RDBMS I use supports != operator.
AJ
Interesting, just checked and SQL Server and MySQL support !=
Andomar
Please consider upvoting my comment then :)
AJ
+1 Hehe alright since I learned something new from your post :)
Andomar
`!=` is ANSI standard. It's been supported in Oracle since 9i; MySQL since at least 4.1
OMG Ponies
@OMG Ponies: No, `<>` is ANSI standard. `!=` is supported by most RDBMS brands, but it's not specified in ANSI SQL.
Bill Karwin
See http://stackoverflow.com/questions/723195/should-i-use-or-for-not-equal-in-tsql/723426#723426
Bill Karwin
+2  A: 

Maybe just

SELECT *
FROM 
    Users
WHERE
    UserId <> @ThisUserId

Or using a Difference Union (The EXCEPT keyword in SQL Server, not sure about other RDBMS implementations)

SELECT *
FROM 
    Users

EXCEPT

SELECT *
FROM 
    Users
WHERE
    UserId = @ThisUserId
Russ Cam
+1 Didn't know about the EXCEPT keyword, cool
Andomar
The INTERSECT keyword can also be extremely useful too, for finding results common to all resultsets
Russ Cam
+1  A: 

If you know what that user's unique ID is you could use something like this for example:

SELECT * FROM usertable WHERE id!='myuserid'

What I do with one of my authentication scripts is store the information for the person that is currently logged in in a variable so it would look like this in PHP:

SELECT * FROM usertable WHERE id!='check(id)'
jeerose
+1  A: 
select * from Foo where UserName not in ('Rohan', 'Rohan's friend', .....)

Is this useful?

Saar