Results 1 to 2 of 2
  1. #1
    hlm15 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Posts
    3

    Query for Many to Many Relationships

    Hi,

    I am trying to set up a query based on a many to many relationship. I have attached a simplified version of my database. Currently I have a table recording information about Songs. As one song could be published for both the piano and the harp and the piano and harp are linked to more than one song, I have set this up using a junction table. For my query I want to display all the songs which are for the piano only. I have tried entering "Piano" into the criteria, but this results in the query displaying all the records which are for both the piano and the harp. What I really need, is to find a way to limit the criteria so that only the songs which has only one ID from the instrumentation table attributed to it (ie, just the piano). Is there a way to do this?



    I do also need a list of all the songs which are for piano and harp, but without duplication the song ID. At present, if a song has been published for both the piano and the harp, the song also appears twice in the query. Is there a way to avoid this?


    Thanks for your help.
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    For Piano query, could use DCount and apply criteria where the count = 1. Also need to change the jointypes of the query:
    SELECT Instrumentation.Instrumentation, Song.Composer, Instrumentation.Instrumentation, Song.[Song Title], Song.LibrarySigla
    FROM Song RIGHT JOIN (Instrumentation RIGHT JOIN Song_Instrumentation ON Instrumentation.ID = Song_Instrumentation.Instrumentation) ON Song.ID = Song_Instrumentation.SongID
    WHERE (((Instrumentation.Instrumentation)="Piano") AND ((DCount("SongID","Song_Instrumentation","SongID=" & [Song.ID]))=1));

    For the Piano/Harp query, have to use the Instrumentation field as criteria only and don't retrieve the field and use either DISTINCT keyword or a GROUP BY (totals) query. Again, need to fix the jointypes:

    SELECT DISTINCT Song.[Song Title], Song.Composer, Song.LibrarySigla
    FROM Song RIGHT JOIN (Instrumentation RIGHT JOIN Song_Instrumentation ON Instrumentation.ID = Song_Instrumentation.Instrumentation) ON Song.ID = Song_Instrumentation.SongID
    WHERE (((Instrumentation.Instrumentation)<>"Guitar" And (Instrumentation.Instrumentation)<>"Lyre"));
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. one-to-one relationships
    By bonecone in forum Database Design
    Replies: 3
    Last Post: 01-25-2012, 02:34 PM
  2. Query Relationships
    By reidn in forum Queries
    Replies: 2
    Last Post: 06-29-2011, 05:40 AM
  3. Relationships again
    By BarbT in forum Access
    Replies: 2
    Last Post: 11-09-2010, 04:12 AM
  4. Too Many Relationships
    By MikeT in forum Database Design
    Replies: 4
    Last Post: 08-25-2010, 07:23 PM
  5. Relationships?
    By rosh41 in forum Database Design
    Replies: 2
    Last Post: 06-23-2010, 01:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums