简体   繁体   中英

How do you join two tables together without duplicating results?

I have two tables in a SQL database in which one references the other. One table is cpddata and the other is 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.

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.

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. Is there a "correct" method to achieving this?

USE DISTINCT

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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