Results 1 to 3 of 3

Thread: SQL query between 2 tables linked by many to many relationship

  1. #1
    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?

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    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) 
      ;

  3. #3
    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.

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,394,781.38876 seconds with 16 queries