简体   繁体   English

简单的MYSQL数据查询?

[英]Simple MYSQL data query?

Just a really simple question. 只是一个非常简单的问题。 In my SQL database I have a column named "friend_count" in table "users" to record all friends the logged in user has. 在我的SQL数据库中,“用户”表中有一列名为“ friend_count”的栏,用于记录登录用户拥有的所有朋友。 This works with UserID numbers and I am trying to figure out how to record them all. 这适用于UserID号,我正在尝试弄清楚如何记录它们。 Here's my example: 这是我的示例:

USER #29 becomes friends with USER #422 USER#29与USER#422成为朋友

In the user database under USER #29's info in the column "friend_count" there will then be the number "422", if he then becomes friends with USER #500, it will show "422, 500" and so on... If he deletes one, that particular number is removed from the box. 在用户数据库中,在“ USER#29”信息下的“ friend_count”列中,将存在数字“ 422”,如果他随后与USER#500成为朋友,它将显示“ 422,500”,依此类推...他删除了一个,那个特定的数字从框中删除了。 The script will then search these numbers through the user database to show a list of the people you are friends with. 然后,脚本将通过用户数据库搜索这些号码,以显示您与之成为朋友的人的列表。

How can I do this? 我怎样才能做到这一点? Any ideas? 有任何想法吗? Thanks! 谢谢!

Any time you have a delimited list of values in a column it's almost always a sign that the data model is incorrect. 每当您在列中有定界的值列表时, 几乎总是表示数据模型不正确。 (Actually, I can't think of an exception to this, but I'll stick with "almost always" just to be safe.) (实际上,我无法想到一个例外,但是为了安全起见,我会坚持“几乎总是”)。

In this case you have two types of entities: 在这种情况下,您有两种类型的实体:

  • User 用户
  • Friendship 友谊

A friendship, though not a physical object, is a conceptual entity in and of itself. 友谊虽然不是物理对象,但它本身就是概念实体。 It connects two users and can add more information related to the friendship itself but not necessarily to the two users. 它可以连接两个用户,并且可以添加有关友谊本身的更多信息,但不一定要添加到两个用户。

So you might have tables like this: 因此,您可能会有这样的表:

User
--------
ID
Name
etc.

Friendship
--------
ID
OriginatingUser
AcceptingUser
BecameFriendsOn
etc.

So OriginatingUser might be the user who sent the friend request, and AcceptingUser might be the user who accepted it. 因此, OriginatingUser可能是发送朋友请求的用户, AcceptingUser可能是AcceptingUser请求的用户。 BecameFriendsOn is the date it was accepted. BecameFriendsOn是接受日期。 You'd probably want to have statuses and other dates to keep track of pending requests, denied requests, etc. But all of this information is related to the friendship, not necessarily to the users. 您可能希望拥有状态和其他日期来跟踪待处理的请求,被拒绝的请求等。但是所有这些信息都与友谊有关,而不一定与用户有关。

The concept you're looking to understand here is called a Foreign Key. 您想在此处理解的概念称为外键。 The OriginatingUser and AcceptingUser columns are the same data type as the ID column on the User table. OriginatingUserAcceptingUser列与User表上的ID列具有相同的数据类型。 You would create the Friendship table such that those columns are foreign keys to the User table, this enforces the integrity of the data so that you can't create a Friendship record without two valid and existing User records. 您将创建Friendship表,使这些列成为User表的外键,这将增强数据的完整性,因此,如果没有两个有效的现有User记录,就无法创建Friendship记录。

Then to get the list of friends, you'd join the tables in a query. 然后,要获取朋友列表,您可以将表加入查询中。 Perhaps something like this: 也许是这样的:

SELECT
  User.Name
FROM
  Friendship
  INNER JOIN User ON Friendship.AcceptingUser = User.ID
WHERE
  Friendship.OriginatingUser = ?

When supplied with the ID of the originating user, this would get all of the names of users to whom that user sent a friend request. 当提供原始用户的ID时,它将获得该用户向其发送好友请求的所有用户的名称。 You can further build on the query to also get users who sent this same user a friend request, and so on. 您可以进一步在查询的基础上,还获得向该用户发送好友请求的用户,依此类推。 By making use of the key foreign key relationships between tables which represent different types of entities in the system, you can construct very robust queries to view that data in lots of different ways. 通过利用表示系统中不同类型实体的表之间的键外键关系,您可以构建非常强大的查询以多种不同方式查看该数据。

You are describing a relationship between two entities so you can create a table to store the details of that relationship. 您正在描述两个实体之间的关系,因此可以创建一个表来存储该关系的详细信息。

Suppose that you have your user table with a userid column and other columns. 假设您的用户表包含一个userid列和其他列。 You can then create a friends table with two columns that are both foreign keys to the user table. 然后,您可以创建一个包含两列的朋友表,这两列都是用户表的外键。

      friends = (user, friend)

Thus, for each friend that user #29 gets you need to add a row into the friends table. 因此,对于用户#29获得的每个朋友,您需要在friends表中添加一行。 For example: 例如:

USER
    ID   NAME  ...
    29   Sam
    30   Henry
    32   Jane


    Friends
    user     friend
    29       30
    29       32

Sam is friends with both Jane and Henry but Jane and Henry are not friends. 山姆是简和亨利的朋友,但简和亨利不是朋友。

我将创建一个带有两个列“用户”和“朋友”的表“友谊”,然后开始添加用户/朋友ID对

users table: id, username, whatever else 用户表: ID,用户名或其他

friends table: relationship_id, user_id, user_friend_id 朋友表格: relationship_id,user_id,user_friend_id

example query to get a list of IDs that belong to the users friends: 示例查询以获取属于用户好友的ID列表:

SELECT f.user_friend_id FROM users u 
LEFT JOIN friends f ON f.user_id = u.id
WHERE u.id = {$user_id}

A very simple approach, assuming that if UserA becomes friends with UserB , then UserB also becomes friends with UserA . 一种非常简单的方法,假设如果UserAUserB成为朋友,那么UserB也与UserA成为朋友。

Usually, Comma Separated Lists are not recommended, as they will become a pain when the list is very large. 通常,不建议使用逗号分隔列表,因为当列表很大时,它们会很麻烦。 A simpler approach will be, make a table friends with columns user_id and friend_id Where user_id and friend_id are the respective UserIDs . 一种更简单的方法是,使表中的列成为user_idfriend_id friends ,其中user_id和friend_id是各自的UserID。

Now when you want to add a friend to someone's list, use the following : 现在,当您要将朋友添加到某人的列表时,请使用以下命令:

INSERT INTO users (user_id,friend_id) VALUES(UserA,UserB),(UserB,UserA)

Now when you execute the above query, you will have 2 new rows in your friends table : 现在,当您执行上述查询时,您的friends表中将有2个新行:

user_id friend_id user_id friend_id

UserA UserB UserB UserA 用户A用户B用户B用户A

When you want to get the list of a user's friends, use : 如果要获取用户的朋友列表,请使用:

SELECT friend_id FROM friends WHERE user_id=(Your user's ID)

This will return a row one by one, and hence will give you all the IDs of friends of a particular user. 这将返回一行一行,因此将为您提供特定用户的所有朋友的ID。

Now when you want to delete a friend, use : 现在,当您要删除朋友时,请使用:

DELETE FROM friends WHERE (user_id,friend_id) IN ((user_id,friend_id),(friend_id,user_id))

This removes both the rows from the table, which means that the relationship between UserA and UserB is deleted, and neither of them is friend of each other. 这将从表中删除这两个行,这意味着UserA和UserB之间的关系被删除,并且它们都不是彼此的朋友。

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

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