[英]Find values missing on one table which are present on another based on two fields
[英]How to find which fields in one table match criteria based on another
我有两个表格: sections
和samples
。 只是要求我增加查看任何部分是否具有与之相关联的样本的功能。
为此,我需要使用samples
表中的字段Job
, ID
和SectionNumber
,并将它们与sections
表中的相同字段进行匹配。
我还需要确定是否应该在名为hasSamples
sections
创建一个新字段并进行维护。
样本数据:
samples table: `Job | ID | SectionNumber | Type | Tests`
samples: `J1302, JC310, 2, Bag, Stress`
表格: Job | ID | SectionNumber | Depth
Job | ID | SectionNumber | Depth
sections: `J1301, JD493, 1, 128`
`J1302, JC310, 2, 50`
所需的结果可能是这样的:
`Job | ID | SectionNumber | Samples` `J1302, JC310, 2, 1`
任何建议表示赞赏。
SELECT * FROM sections se
LEFT JOIN samples sa ON sa.Job = se.Job AND sa.ID = se.ID, and sa.SectionNumber = sel.SectionNumber
WHERE sa.ID IS NOT NULL
在初始设计中,可能没有令人信服的理由拥有诸如hasSamples之类的“计算的”或“缓存的”字段,因为JOIN显然是微不足道的(假定您正确定义了所有正确的索引和外部关系)。 但是,如果您决定避免超级频繁的JOIN可以帮助提高性能,那么就没有理由不添加列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.