简体   繁体   中英

Filter one table based on values from another table column

I have a table "PackagingType" with 2 columns (materialID | name) and another table "materials" with ID and material details.

For each name from the "packagingtype" I want to assign what materials are available (eg in material ID column I would have 1,3,4,5).

I need to match these materialsID with materials table and pull out the details.

What would be the correct way to do it? Not sure if storing data as 1,3,4,5 is the right was and what would be the syntax look like?

In relational database design you don't combine values into one cell. There are exceptions but are few and far between. This would would not be normalized data and would make future query and analysis difficult/complex. So the PackagingType should have the same name multiple times for different materialIDs.

So the table would have data like

MaterialID Name
1          PackageA
3          PackageA
4          PackageA
5          PackageA

Then to get results with the material description you'd simply do a join.

SELECT PT.MaterialID, PT.Name, M.Detail
FROM PackagingType PT
INNER JOIN Materials M
 on PT.MaterialID = M.MaterialID

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