Filtering a Selected Column in SQL
I have the following table user_user
with fields (userid1
, userid2
)
I want to select all users that are related to a specific user X, whether or not they are in the first or second column. But I only want to select the other users...i.e. I don't want X to be part of the result set.
How does 开发者_JAVA技巧one do this?
SELECT * FROM user_user WHERE userid1 = X.id OR userid2=X.id
will give pretty much a sub-table that is not a straight forward list of users in relationship with X and needs some post processing...is there a more direct SQL command?
SELECT userid1 AS relateduserid
FROM user_user
WHERE userid2 = XID
UNION
SELECT userid2
FROM user_user
WHERE userid1 = XID
SELECT * FROM user_user
WHERE (Not userid1 = @X) AND (Not userid2=@X)
This should get all records that do not contain user in either field:
SELECT U0.*
FROM user_user U0
LEFT OUTER JOIN USER_USER U1
ON U1.userID1 = U0.userID1
OR U1.userID1 = U0.userID2
OR U1.userID2 = U0.userID1
OR U1.userID2 = U0.userID2
WHERE U1.userID1 IS NULL
AND ( U0.userID1 = <x>
OR U0.userID2 = <x>
)
精彩评论