简体   繁体   English

如何在不重复结果的情况下将两个表连接在一起?

[英]How do you join two tables together without duplicating results?

I have two tables in a SQL database in which one references the other.我在 SQL 数据库中有两个表,其中一个引用另一个。 One table is cpddata and the other is subdb.一张表是cpddata,另一张是subdb。 Each entry in cpddata may have between 0 and 50 subdb entries that reference the id from cpddata, for example, if I create an entry in cpddata and it's id number is 30, I have a column in subdb that is named cpdid and each subdb entry that relates to the cpddata entry will be assigned the 30 value. cpddata 中的每个条目可能有 0 到 50 个引用 cpddata id 的 subdb 条目,例如,如果我在 cpddata 中创建一个条目并且它的 id 号是 30,我在 subdb 中有一个名为 cpdid 的列和每个 subdb 条目与 cpddata 条目相关的值将被分配 30 值。

In the past when I have had to reference one table to another table I have used SQL joins but if I join cpddata and subdb on cpdid then I get a duplicated result output for every subdb that is associated with the cpddata entry.过去,当我不得不将一个表引用到另一个表时,我使用了 SQL 连接,但是如果我在 cpdid 上连接 cpddata 和 subdb,那么我会为与 cpddata 条目关联的每个 subdb 获得重复的结果输出。

What I would like to be able to accomplish is to return the single cpddata entry but still have the subdb data attached to the correct cpddata entry and be filterable with AngularJS.我希望能够完成的是返回单个 cpddata 条目,但仍然将 subdb 数据附加到正确的 cpddata 条目并且可以使用 AngularJS 进行过滤。 Is there a "correct" method to achieving this?有没有“正确”的方法来实现这一目标?

USE DISTINCT使用DISTINCT

SELECT distinct C.ID, S.ID
FROM cpddata C
INNER JOIN subdb S
  ON C.ID = S.cpdid

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

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