Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 09-05-2009
Member
 
Join Date: May 2008
Posts: 962
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?
Reply With Quote
  #2  
Old 09-05-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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) 
  ;
Reply With Quote
  #3  
Old 09-05-2009
Member
 
Join Date: May 2008
Posts: 945
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 View Post
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "SQL query between 2 tables linked by many to many relationship"
Thread Thread Starter Forum Replies Last Post
Foreign key relationship among tables in SQL Satchel Software Development 4 06-01-2011 07:21 AM
Why it takes so much linked Excel tables in an Access DB? Dante i Windows Software 4 03-11-2010 06:26 AM
MSP Task Relationship Update Query johndavo Windows Software 1 30-10-2010 04:42 AM
Join two tables using SQL query vaib288932 Software Development 3 20-08-2010 12:01 PM
Relationship among tables in access Bindusar Windows Software 5 08-01-2010 05:47 AM


All times are GMT +5.5. The time now is 01:50 AM.