简体   繁体   English

存储关注用户的最佳方法

[英]Best way to store followed users

I know the title isn't so describing but it's really hard to find something generic to describe my situation. 我知道标题不是那么描述,但是很难找到通用的描述我的情况的东西。 If someone wants to edit, feel free... 如果有人想编辑,请随意...

So, I have a postgres database, with a users table. 因此,我有一个postgres数据库和一个users表。 I would like to store the users followed by one user, and I really don't see how I could do this. 我想先存储一个用户,再存储一个用户,但我真的不知道该怎么做。 I would like to do like SELECT followed_users FROM users WHERE username='username' and this would return me every usernames, or id, or whatever of each followed users. 我想SELECT followed_users FROM users WHERE username='username' ,这将向我返回每个用户名或id,或每个跟随的用户。 But I don't see any clean way to do this. 但是我看不到任何干净的方法可以做到这一点。

Maybe an example would be more describing: user1 is following user2 and user3 . 也许一个示例将更能说明问题: user1紧随user2user3
How to store who user1 is following? 如何存储user1正在关注的人?

EDIT: I don't know how many users the user will follow. 编辑:我不知道该用户将关注多少个用户。

Thank you for your help. 谢谢您的帮助。

Expanding on my comment above, since it got wordy: 由于上面罗word了,请在上面扩展我的评论:

Create a new table called something like user_follows with columns like 创建一个名为user_follows类的新表,并添加如下列

user_id1 | user_id2 

or 要么

follower_id | follows_id 

Then you can query: 然后您可以查询:

SELECT t1.username as follower_username, t3.username as following_usernae
FROM users t1
    INNER JOIN user_follows t2 ON t1.user_id = t2.follower_id
    INNER JOIN users t3 ON t2.following_id = t3.user_id
WHERE t1.user_id = <your user>

In the end, think of your tables as "Objects". 最后,将表视为“对象”。 Then when you are presented with a problem like "How do I add users that are following other users" you can determine if this relationship is a new object, or an attribute of an existing object. 然后,当出现诸如“如何添加跟随其他用户的用户”之类的问题时,可以确定此关系是新对象还是现有对象的属性。 Since a user might follow more than one other user than the relationship is not a good attribute for "Users", so it gets its own table user_follows . 因为一个用户可能跟随一个以上的其他用户,但对于“ Users”而言,关系不是一个好属性,因此它获得了自己的表user_follows

Since user_follows is just one type of relationship that two users may have to one another, it might make sense to increase the scope of that object to relationships and store the relationship type as an attribute of the table: 由于user_follows只是两个用户可能必须彼此之间的一种关系类型,因此有可能将该对象的范围扩大到relationships并将该关系类型存储为表的属性可能是有意义的:

user_id1 | user_id2 | relationship_type

where relationships.relationship_type might have values like follows, student of, sister of etc... 其中relationships.relationship_type可能具有以下值follows, student of, sister等...

So the new query would be something like: 因此,新查询将类似于:

SELECT t1.username as follower_username, t3.username as following_username
FROM users t1
    INNER JOIN relationships t2 ON t1.user_id = t2.user_id1
    INNER JOIN users t3 ON t2.user_id2 = t3.user_id
WHERE t1.user_id = <your user> AND t2.relationship_type = 'Follows';

I'd add another table, let's call it following for argument's sake, which saves pairs of users and users they are following: 我要添加另一个表,为了参数的缘故,我们将其称为“ following ”,它可以节省成对的用户和正在关注的用户:

CREATE TABLE following (
    user_id INT NOT NULL REFERENCES users(id), 
    following_id INT NOT NULL REFERENCES users(id),
    PRIMARY KEY (user_id, following_id)
)

Then you could query all the user's a specific user is following by joining with the users table (twice). 然后,可以通过与users表联接(两次)来查询所有用户的特定用户。 Eg, to get the names of all the users that I (username "mureinik") am following: 例如,要获取我以下所有用户的名称(用户名“ mureinik”):

SELECT fu.username
FROM   following f
JOIN   users u ON f.user_id = u.id
JOIN   users fu ON f.user_id = fu.id
WHERE  u.username = 'mureinik'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM