Hello! There is a friends table, fields in it: id, user_1, user_2. user_1 This is the user who offered the friendship, and user_2 who agreed. It is necessary to make a sample that will show the user's friends, but we do not know who the user is ... the initiator of the friendship or the one who agreed to it.
3 answers
In my opinion, your table lacks another field "status". When someone offers friendship, a record is made in this table. user_1 is the one who offered the friendship, user_2 is the one to whom the friendship was offered and the status is await. If the second agrees, then the status is changed to friendship_forever. Then you will not get confused, because user_1 - it will always be the first to offer friendship.
PS I figured it out, but what difference does it make - I suggested friendship or agreed with the proposal? Indeed, in any case, two people become friends and, accordingly, what is needed in the list of friends to show everyone.
- I need to bring his friends, and if I organize a cycle with two fields, he will see himself on his list - angers777
- oneIn general, here is this reference , sketched you a working example. - Deonis
"select * from friends where user_1=$id or user_2=$id"
UPD:
"select user_1 from friends where user_2=$id union all select user_2 from friends where user_1=$id"
- And how can I organize a loop with such a sample ?? while ($ row = mysql_fetch_assoc ($ result)) {echo $ row ['user_1']; echo $ row ['users_2']; } But alas, the user himself, who wants to see his friends, will be on the list himself, if is also not an option, because generally I’m trying to cram it all into an array - angers777
- one
union
will be faster thanor
, almost certainly. UNION ALL vs OR condition in sql server query is the second part of the response, wherecompany or city
. - Yura Ivanov
To unite those who responded to Vasya's suggestions, and those to whose proposals Vasya agreed.
$query = sprintf( 'SELECT `user_1` AS userid FROM `friends` WHERE `user_2`=%1$d UNION SELECT `user_2` AS userid FROM `friends` WHERE `user_1`=%1$d', $id );