SQL query between 2 tables linked by many to many relationship
I have a parent class with a first name, last name, and a collection of "children". Put the child class that is just a name.
The two classes are connected by a link "many to many" (1 .. n).
I run a query: "give me all those who have children named John and Mary." So I will have all the result of people who have at least two children, a John and Mary. Logically it is correct.
Is it too complex?
Re: SQL query between 2 tables linked by many to many relationship
According to me, the SQL query should look like this:
Code:
SELECT *
FROM Parent AS x
WHERE EXISTS (SELECT *
FROM (SELECT y.Parentid
FROM Child AS y INNER JOIN Child AS z
ON y.ParentId = z.ParentId
WHERE y.FirstName = 'John' AND z.FirstName = 'Mary') AS t
WHERE x.Parentid = t.Parentid)
;
Re: SQL query between 2 tables linked by many to many relationship
Inappropriate because your statement is false in the database:
Quote:
Originally Posted by
Luis234
The two classes are connected by a link "many to many" (1 .. n).
1 .. n means link 1 to many.
many-to-many means many to many
So the two concepts are altogether different.