Table structure for PHP friendship systems

Asked

Viewed 479 times

2

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

I’ve thought of something:

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

However I do not know if it is the best way to do and much less how could do the query relationship to know if a user is a friend of another user.

What’s the best way to do it and how to do it?

  • 1

    First you should check if a user can also be a friend, if yes, I fear that a self relationship might come to solve your problem.

1 answer

2


This is the simplest way having user and friend. In the query you check whether User ID matches the ID of friend and vice versa, in usuario or in usuarioamigo

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

reference 1 , reference 2

  • I think I can understand... so this organization of the table amigos is correct?

  • 1

    @Igor, yes it is correct, I see no reason to avoid.

  • It would not be a good idea to use two Inserts? INSERT INTO amigos values(amigo_id, usuario_id) VALUES (70, 80), (80, 70). Because I had problems with my server (I think because of OR), that is making everything very slow!

  • @Wallacemaxters is also an option, but in this case will be many duplicate lines.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.