Table structure for PHP friendship systems

2

I am developing a friends system in my system and would like to know how best to organize the table that will take care of such records amigos :

I've thought of something like this:

Tabela "amigos" (apenas 3 colunas)
-----------------------------------
   id | usuario | usuarioamigo

But I do not know if this is the best way to do it, let alone how you could do the query relationship to know if a user is friends with another user.

What is the best way to do this and how to do it?

    
asked by anonymous 25.04.2015 / 17:46

1 answer

2

This is the simplest form having user and friend . In the query you check whether User ID matches the Friend ID and vice versa, either usuario or usuarioamigo

select * from amigos
where ( usuario = $IDUser or usuarioamigo = $IDUser )
  and ( usuario = $amigo  or usuarioamigo = $amigo  )

reference 1 , reference 2     

25.04.2015 / 18:03