Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - identifying exact matches across multiple records

Tags:

sql

ms-access

Table Parent

Column1
S1
S2
S3

Table Child

Column1     Column2
S1          P1
S1          P2
S2          P1
S2          P2
S3          P1

Where parent.column1 = child.column1

Given the above tables, I need to identify the parents whose children have the same records in column2 as parent S1 does.

For example, S1 and S2 both have P1 and P2, so that would meet the condition. S3, however, does not have P2, and should therefore be excluded.

New to SQL, so I'm having some trouble. Tried it by using a not in statement, but since S3 has P1, it's not being excluded.


1 Answers

You need a join. This will vary by SQL dialect. Something like:

select child.column1, child.column2 from (
  select column2 as parentsColumn2Value from child where column1='S1'
) as parentsColumn2Table
left join child on parentsColumn2Table.column2=child.column2
like image 125
jwl Avatar answered Dec 03 '25 21:12

jwl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!