简体   繁体   English

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

[英]How do I create a “link” between two mySQL tables?

I have two tables that look as Follows: 我有两个表,如下所示:

Person (Table Name) 人(表名)

Name1/Phone1/Email1/Address1/Organization1/Notes1 (Fields) 名称1 /电话1 /电子邮件1 /地址1 /组织1 /注释1(字段)

Organization (Table Name) 组织(表名称)

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

Organization1 is the only field in common between the two tables. Organization1是两个表之间唯一的共同字段。

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. 我正在使用PHP与mySQL交互。

You need to JOIN the tables. 您需要JOIN表。

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 . 自从你说如果存在以来,我已经完成了LEFT JOIN 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. 注意:我同意并建议通过添加主键并将其用作其他表中的外键来使数据库更具关系。

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

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. 有3种关系。

1) One -> One - This type of relation means 1 row is related to 1 other row in a different table 1)一->一-这种类型的关系意味着另一行中的另一行与另一行相关

2) One -> Many - This type of relation means 1 row is related to a variable number of rows in a different table. 2)一->多-这种关系意味着1行与另一张表中可变数量的行相关。
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. 因此,在这种情况下,1将是文件夹,而许多将是文件。

3) Many -> Many - This type of relation means many rows can relate to many other rows. 3)很多->很多-这种关系意味着很多行可以与很多其他行相关。
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 . Tizag在这里有关于联接的出色教程: http : //www.tizag.com/sqlTutorial/sqljoin.php

Hope that helps. 希望能有所帮助。

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). Person_ID,姓名,电话,电子邮件,地址,Organization_ID,便笺(或者,如果您有多个便笺,请创建一个单独的表,将person_id映射到便笺)。

Organisation_ID, Name, Phone, Email, Address, Web, Notes. Organisation_ID,姓名,电话,电子邮件,地址,Web,注释。

Select your person, then if Organisation_ID exists, select the Organisation where Organisation_ID equals the ID you obtained from the person row. 选择您的人员,然后如果Organisation_ID存在,请选择Organisation_ID等于您从人员行获得的ID的组织。

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

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