如何创建“链接”两个 mySQL 表之间?

发布于 2024-11-08 17:25:14 字数 293 浏览 5 评论 0原文

我有两个表,如下所示:

人员(表名称)

Name1/Phone1/Email1/Address1/Organization1/Notes1(字段)

Organization(表名称)

Organization1/Phone2/Email2/Address2/Web2/Notes2(字段)

Organization1 是两个表之间唯一的共同字段。

当我显示某个人的数据时,我还想检查并查看其组织是否有数据,如果存在则也显示它。我正在使用 PHP 与 mySQL 交互。

I have two tables that look as Follows:

Person (Table Name)

Name1/Phone1/Email1/Address1/Organization1/Notes1 (Fields)

Organization (Table Name)

Organization1/Phone2/Email2/Address2/Web2/Notes2 (Fields)

Organization1 is the only field in common between the two tables.

When I display data on a person, I want to also check and see if there is data on their organization and display it as well if it exists. I'm using PHP to interface with mySQL.

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

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

发布评论

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

评论(4

塔塔猫 2024-11-15 17:25:14

您应该使用 外键 ,但需要使用InnoDB存储引擎(MyISAM尚不支持外键)。

You should use a foreign key, but you need to use the InnoDB storage engine (MyISAM does not support foreign keys yet).

烙印 2024-11-15 17:25:14

您需要JOIN这些表。

SELECT * FROM Person LEFT JOIN Organization ON Person.Organization = Organization.Name;

这假设关系是组织名称。自从你说如果存在以来,我已经完成了LEFT JOIN。有关连接表的更多详细信息,请查看本教程

注意:我同意并建议通过添加主键并将它们用作其他表中的外键来使您的数据库更加相关。

You need to JOIN the tables.

SELECT * FROM Person LEFT JOIN Organization ON Person.Organization = Organization.Name;

This assumes the relationship is the Organization Name. I've done a LEFT JOIN since you said if exists. Check out this tutorial for more detail on joining tables.

Note: I agree and would recommend making your database more relational by adding Primary Keys and using them as Foreign Keys in your other tables.

野鹿林 2024-11-15 17:25:14

这篇文章是关系的解释,不是供您使用的代码。如果您想要这样,请看看其他地方
那么,表之间的连接称为关系。有 3 种类型的关系。

1) 一->一 - 这种类型的关系意味着 1 行与不同表中的另外 1 行相关

2) 一 -> 1许多 - 这种类型的关系意味着 1 行与不同表中的可变数量的行相关。
例如,一个文件夹可以有多个文件,但一个文件不能有多个文件夹。因此,在这种情况下,1 是文件夹,许多是文件。

3)很多->许多 - 这种类型的关系意味着许多行可以与许多其他行相关。
一个例子可能是标签。您可以为许多东西贴上相同的名称(例如桌面设备),并且每个东西可以有多个标签(一盏灯可以同时具有桌面设备和灯光标签)。

.
现在您已经了解了不同的关系,我们将讨论您的问题。你所看到的关系是一对多的关系,一个公司可以有很多人,但一个人只能拥有一个公司。我想一个人可以为多人工作,但这要复杂得多(所以我们将跳过它)。

一对多关系是迄今为止最常见的,并且很容易实现。这就是连接发挥作用的地方(左连接、右连接和内连接)。 Tizag 在此处提供了有关联接的优秀教程:http://www.tizag.com/sqlTutorial/sqljoin。 php.

希望有帮助。

This post is an explanation of relations, not code for you to use. If you want that, look elsewhere
Well, connections between tables are called relations. There are 3 types of relations.

1) One -> One - This type of relation means 1 row is related to 1 other row in a different table

2) One -> Many - This type of relation means 1 row is related to a variable number of rows in a different table.
An example may be A folder can have multiple files, but a file can't have multiple folders. So in this case the 1 would be the folder, and the many would be the files.

3) Many -> Many - This type of relation means many rows can relate to many other rows.
An example may be labels. You can label many things the same name (desk appliance for example), and each thing can have multiple labels (a lamp can have both desk appliance & light labels).

.
So now that you know the different relations, we will go into your question. The relation you are looking at is a one to many, one corporation can have many people, but a person can only have one corporation. I suppose a person could work for multiple people, but that is much more complex (so we'll skip it).

One to many relations are by far the most common, and are pretty easy to do. This is where joins come in (left, right, and inner joins). Tizag has an excellent tutorial on joins here: http://www.tizag.com/sqlTutorial/sqljoin.php.

Hope that helps.

波浪屿的海角声 2024-11-15 17:25:14

让您的表格看起来像这样:

Person_ID、姓名、电话、电子邮件、地址、Organization_ID、注释(或者,如果您有多个注释,请创建一个单独的表,将 person_id 映射到注释)。

Organization_ID、姓名、电话、电子邮件、地址、网址、注释。

选择您的人员,然后如果 Organization_ID 存在,请选择 Organization_ID 等于您从人员行中获取的 ID 的组织。

Make your tables look something like this:

Person_ID, Name, Phone, Email, Address, Organisation_ID, Notes (or if you have multiple notes, create a seperate table that maps person_id to a note).

Organisation_ID, Name, Phone, Email, Address, Web, Notes.

Select your person, then if Organisation_ID exists, select the Organisation where Organisation_ID equals the ID you obtained from the person row.

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