For the one who down voted the question, could you please explain to me the reason? If this seems trivial to you, I would appreciate you pointing me to a reference; If this is a duplicate question, I would appreciate you pointing me to the earlier question. Thanks!
I have two tables, user
and friend
. The goal is as follows: A user a
wants to get the information of another user b
; the returned info depends on whether they are friends or not. To be concrete, if they are friends, a
can see b
's username
and address
; if not, a
can only see b
's username.
user
table looks like:
id username address
1 abc XXXX Rd. XXXX
2 def XXXX Rd. XXXX
3 ghi XXXX Rd. XXXX
friend
table looks like:
id id1 id2
1 1 2 // so 1 (abc) and 2 (def) are friends
2 1 3 // so 1 (abc) and 3 (ghi) are friends
2 (def
) and 3 (ghi
) are not friends. So basically there are the following two scenarios:
- user 2 (
def
) want to see user 3 (ghi
)'s info, the query returnsghi
's username only but not address - user 1 (
abc
) want to see user 3 (ghi
)'s info, the query returnsghi
's username and address
Is it possible to do it using one query?