Home  >  Q&A  >  body text

mysql - 类似QQ的好友关系表是怎么设计的?

请问一下类似QQ的好友关系表是怎么设计的?
难道只是简单的id,userId,friendId吗?

迷茫迷茫2741 days ago3266

reply all(12)I'll reply

  • 一个人流浪

    一个人流浪2018-06-13 16:52:23

    There should also be a grouping field

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 14:48:10

    In fact, there is no need to overcomplicate things, just progress slowly according to your needs.

    This is the table structure of my follow function, you can refer to it.

      UserRelationship:
        type: object
        properties:
          id:
            type: integer
            description: Id
          user_id:
            type: integer
            description: 用户Id
          target_user_id:
            type: integer
            description: 目标用户Id

    reply
    0
  • 黄舟

    黄舟2017-04-17 14:48:10

    Non-relational database

    reply
    0
  • 阿神

    阿神2017-04-17 14:48:10

    When I was designing here, I considered the function of groups, so I also converted the friend relationship between two people into a group

    Three tables should appear

    One is the user table
    One is the group table
    The other is the user-group correspondence table

    Determined through three tables

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 14:48:10

    As far as I know, Weibo’s attention is designed this way

    reply
    0
  • 迷茫

    迷茫2017-04-17 14:48:10

    I have seen an interview question before, which is about table design. How to design the friend relationship table? In the user table, use a field separated by commas to store it or store it in two tables in association. It seems that both are feasible

    reply
    0
  • PHPz

    PHPz2017-04-17 14:48:10

    This is enough

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:48:10

    But querying is also a problem. Isn’t redis very suitable for this task?

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 14:48:10

    Maybe you can add a flag field to indicate whether you are a two-way friend

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 14:48:10

    Should be a many-to-many relationship.
    A user can have multiple friends.
    You can also be added as friends by multiple users.
    Many-to-many relationships, in relational databases, are generally implemented using intermediate tables.
    At this time, the intermediate table usually only stores user IDs and friend IDs. But for the convenience of business implementation, you can add verification pass, friend group ID, sorting number, etc. to the intermediate table.
    The difference between this many-to-many intermediate table and the general many-to-many is that the association table of this is itself. That is, the many-to-many relationship between the user table and the user table.

    reply
    0
  • Cancelreply