Would a subquery be ideal to check friendship of users? (and if they are friends back)
I basically have a friend
table with two primary columns, uid and fid (user/friend id's) and they could go either way, for example a user could befriend somebody and have an entry, although the friend may not have befriended the specific user back and I would like to check for this.
Untested, however I envisioned something like this to work:
SELECT fid FROM friends WHERE uid = 20 -- check friends开发者_开发问答 of uid 20
AND
(SELECT uid FROM friends WHERE fid = 20)) = 0
I assume it isn't as easy as this (and if 0 can really check the result or if it is needed), however do you have any suggestions on how to do this without subqueries? Or is this the best "hack" that could be done without two separate queries and result checking code in my server side language?
I would also like to check if they are both friends and whatnot in the future, so I could make use of a better example from here.
look up UNION
, UNION ALL
, and NOT IN
these will help you in your set comparisons.
精彩评论