简体   繁体   English

蓬勃发展:如何选择一对多

[英]Flourish: How to select one-to-many

Let's suppose there is a set of A and for each a of A there is a set of B. In short: A:B = 1:n 假设有一组A,并且每个A都有一组B。简而言之:A:B = 1:n

Let's suppose, there is an a, which is an A and it is interesting. 假设有一个,它是一个A,这很有趣。 However, I would like to select a with all its B elements. 但是,我想选择一个包含所有B元素的a。

For instance, let's consider the example of Companies and Workers. 例如,让我们考虑“公司和工人”的示例。 For the sake of simplicity, we assume that a Worker works at a single Company and a Company might have a natural number of Workers. 为简单起见,我们假设一个工人在单个公司工作,而一个公司可能有自然数量的工人。

If I want to select a set, containing the values of Company.Name, Worker.Name of a given Company, then what should I do in PHP, to achieve the expected result? 如果要选择一个包含给定公司的Company.Name,Worker.Name值的集合,那么我应该在PHP中做什么以达到预期的结果?

Based on your example, assuming you have this schema 根据您的示例,假设您具有此架构

Table Company 餐桌公司

  • CompanyID (PK) 公司编号(PK)
  • CompanyName 公司名
  • other columns.. 其他列..

Table Worker 餐桌工人

  • WorkerID (PK) 工号(PK)
  • WorkerName 工人名
  • CompanyID (FK) 公司编号(FK)

You can join the tables by linking Company and Worker with CompanyID 您可以通过将CompanyWorkerCompanyID链接来加入表

SELECT  a.Name CompanyName,
        b.Name WorkerName
FROM    Company a
        INNER JOIN Worker b
            ON a.CompanyID = b.CompanyID

To further gain more knowledge about joins, kindly visit the link below: 要进一步获得有关联接的知识,请访问以下链接:

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

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