There are several ways, with different levels of performance and ease.
SELECT utilizador.* FROM utilizador WHERE id IN (SELECT user_2_id FROM amigos WHERE user_1_id = '6');
In this case user_2_id would always have to be the friend. While user_1_id would always be the same as the connected user. In this case whenever you add someone it would be necessary to have two INSERT
.
There is duplicity between friends (user_1_id = 1
and user_2_id = 2
| user_1_id = 2
and user_2_id = 1
) since each one is each one’s friend? If yes, it’s ready!
Case there is no duplicity will have to make some changes, can see below, but not recommended for major performance problems, was just to give an idea and quickly.
SELECT utilizador.* FROM utilizador WHERE (id IN (SELECT user_2_id FROM amigos WHERE user_1_id = '6') OR id IN (SELECT user_1_id FROM amigos WHERE user_2_id = '6')) AND id != '6';
The reason for this is that I can add or your accepted (for example!) what could cause me to be one user_1_id
or user_2_id
.
To get the '6' could use $_SESSION['']
or $_COOKIE['']
or any other similar method, but the question seems to be the Mysql structure and not this.
SELECT * amigos user_1_id = $_SESSION['id']
where the session id variable stores the logged-in user id.. Then when retrieving the data you make another query in the user table byusername
id you got in the first query, or you can do it using JOIN(I think).. Try to put the code you are using, thus increases the chance of proper response.– Leonardo
mysql, postgresql, sqlserver?
– Guilherme Nascimento
All his questions were using Mysql, I believe that if it was another would warn, but good question. I failed to deduce that it would be Mysql. I have to learn not to answer empty questions, but I can’t. =(
– Inkeliz
@Inkeliz but only you have understood my point of view so easily with a comment as vague as mine demonstrates that you are smart and take things easily, give you a hint, always use the button to [close] vague questions, even if it is "silly" thing. Closing is not deleting or banning is just preventing questions that need clarification from receiving answers without reaching the minimum standard of quality ;)
– Guilherme Nascimento
I don’t have access to this function, I believe. If you do I don’t know where it is. Then I poke around the Meta to see if you have anything about it. : S
– Inkeliz