Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to handle Many-to-Many relationships in PHP MySQL
    primarykey
    data
    text
    <p>I am looking for the best way to handle a database of many-to-many relationships in PHP and MySQL.</p> <p>Right now I have 2 tables:</p> <p>Users (id, user_name, first_name, last_name)<br> Connections (id_1, id_2)</p> <p>In the <code>User</code> table <code>id</code> is auto incremented on add and <code>user_name</code> is unique, but can be changed. Unfortunately, I don't have control over the <code>user_name</code> and its ability to be changed, but I must account for it.</p> <p>The <code>Connections</code> table is obviously, user1 and user2's id.</p> <p>The connection table needs to account for these possible relations: </p> <pre><code>user1 --&gt; user2 (user 1 friends with user 2 but not user2 friends with user1) user2 --&gt; user1 (user 2 friends with user 1 but not user1 friends with user2) user1 &lt;--&gt; user2 (user 1 and user 2 mutually friends) user1 &lt;-!-&gt; user2 (user 1 and user 2 not friends) </code></pre> <p>That part is not the problem, The problem I am having with is keeping these relations unique when and if they change in batches.</p> <p>Possible solution 1: delete all of user 1's relations and readd them with the updated list. I think this might be too slow for my needs.</p> <p>Solution 2? Anyone else encounter this problem? How should I best handle this?</p> <p>update: distinguishing relationships:</p> <p>i handle relationships like this:</p> <pre><code>user1, user2 user1, user3 user2, user1 </code></pre> <p>in that example the following is true:<br> user1 follows user2 and user3<br> user2 only follows user1 but doesn't follow user3<br> user3 doesn't follow either user1 or user2 </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload