在 MySql 数据库中存储人与人之间的联系

发布于 2024-08-26 18:18:10 字数 176 浏览 1 评论 0原文

我想创建一个数据库来存储人与人之间的链接。

然后,我需要带回详细记录哪些人与哪些人有联系的记录。

我需要输出: person1 isconnected to person2 isconnected with person3 等等。

我如何开始执行此任务?

I would like to create a database where I store the links between people.

I then need to bring back the records detailing which people are connected to which others.

I need to output: person1 is connected to person2 is connected with person3 etc.

How can I get started with this task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖 2024-09-02 18:18:10

好吧,让我假设您的意思是您想要找出 MySQL 数据库中某些条目之间的关系。在我看来,理想的方法是拥有用户表,在本例中我们将其称为 tblusers。我确信您会有很多列,但为了建立联系,我们可以将一列命名为“ucons”,以表示与其他用户的关系。

您可能知道也可能不知道,用户表将有一个主键列,在本例中我将其命名为“uid”。这些主键最终用于定位您的用户条目,因为每个用户都会在主键列“uid”中收到一个唯一的 ID。

假设我们有三个用户:user1、user2、user3,他们具有以下用户 ID:

user1 -> user1 -> user3。 1

用户2 -> 2

用户3 -> 3

现在,对于你关于关系的问题,我通常会将关系键存储在一个单独的表中,这是因为你可能有数千个用户,如果每个用户有 200 个连接,那么表上的开销会很大,特别是同时在表上执行其他操作时!这称为一对多-多对一关系。

现在,让我们有一个秒表,其中包含两列,用于表示人与人之间的实际“连接”,我们将其称为“tblconnections”,其中包含“cid”、“cuser”和“ccon”列。

cid 是主键
cuser 是刚刚创建连接的 tblusers 用户的 ID
ccon 是 tblusers 中 CONNECTED 用户的 ID

因此,我们采用以下两个表布局:

tblusers:

-------------------
uid | uname | uage
-------------------
0   | Joe   | 22
1   | Bob   | 54
2   | Tod   | 14

tblconnections

-------------------
cid | cuser | ccon
-------------------
0   | 1     | 2
1   | 1     | 0

现在,这告诉用户 ID 为“1”的用户有两个连接,一个与用户“2”连接,一个与用户“0”连接在 tblusers 中,因此用户 1(Bob) 与 Tod 和 Joe 都有连接。

有了这些信息,您可以执行一个简单的 mysql 查询,例如:

SELECT uid AS id, (SELECT GROUP_CONCAT(cconn) FROM tblconnections WHERE cuser = id) FROM tblusers;

这将从“tblusers”返回您常用的 iser id 以及所有连接用户的串联字符串。因此,用户 ID 可能是“3”,连接的字符串可能如下所示:2,6,22,45,67,8,在本例中,这是连接到用户“3”的所有用户的列表。

请注意,还有许多其他方法可以返回每个连接的用户信息以及您的查询,这只是一个示例。

如果您想阅读教程,只需谷歌搜索“数据库关系”、“一对多多对一关系”或“mysql 表关系”。

希望这对您的问题有所帮助。

祝你过得愉快。

Ok, let me assume you mean you want to find out the relationships between certain entries in a MySQL database. The ideal way - in my opinion - would be to have your users table which in this case we shall call tblusers. I am sure you'd have many columns, but for this connection sake we can name one column 'ucons' for the relationships with other users.

As you may or may not be aware, the users table will have a primary key column, in this case I will name it 'uid'. These primary keys are ultimately used to target your user entries, as each user receives a unique id in the primary key column 'uid'.

Lets say we have three users: user1, user2, user3 who have the following user ID's:

user1 -> 1

user2 -> 2

user3 -> 3

Now, to your question about relationships, I would usually store the relationship keys in a separate table, this is because you could have thousands of users, and if each user had 200 connections, that is a lot of overhead work on the table, especially if other operations are being performed on the table at the same time! This is known as a one to many - many to one relationship.

Now, lets have a seconds table with two columns for our actual 'connection' between people, we shall call it 'tblconnections' with columns 'cid' 'cuser' and 'ccon'.

cid is a primary key
cuser is the ID of the user from tblusers who has just created a connection
ccon is the ID of the CONNECTED user in tblusers

So lets take the following two table layouts:

tblusers:

-------------------
uid | uname | uage
-------------------
0   | Joe   | 22
1   | Bob   | 54
2   | Tod   | 14

tblconnections

-------------------
cid | cuser | ccon
-------------------
0   | 1     | 2
1   | 1     | 0

This now tells use that user with id '1' has two connections, one to user '2' and one to user '0' in tblusers, so user 1(Bob) has a connection to Tod as well as Joe.

With this info you can perform a simple mysql query such as:

SELECT uid AS id, (SELECT GROUP_CONCAT(cconn) FROM tblconnections WHERE cuser = id) FROM tblusers;

This will return your usual iser id from 'tblusers' as well as a concatenated string of all connected users. So the user id may be '3' and the concatenated string may look like this: 2,6,22,45,67,8 which is the list of all users connected to user '3' in this case.

Please note, there are MANY other methods for returning each connected users info along with your query, this is simply an example.

If you want to read up on tutorials, simply google "database relationships", "one to many many to one relationships" or "mysql table relationships".

Hope this sheds some light on your issue.

Have a good one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文