best way to store 1:1 user relationships in relational database

后端 未结 4 1033
独厮守ぢ
独厮守ぢ 2021-02-01 09:24

What is the best way to store user relationships, e.g. friendships, that must be bidirectional (you\'re my friend, thus I\'m your friend) in a rel. database, e.g. MYSql?

<
4条回答
  •  盖世英雄少女心
    2021-02-01 10:13

    You could check which of the two user_id's is the lowest and store them in a specific order. This way you don't need double rows for one friendship and still keep your queries simple.

    user_id_low | user_id_high

    a simple query to check if you're already friends with someone would be:

    
    

    Or you could find the lowest/higest userid using mysql

    
    

    And to get all your friends id's

    
    

提交回复
热议问题