If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Need help with query - joining tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-28-11, 19:24
webchris webchris is offline
Registered User
 
Join Date: Aug 2011
Posts: 2
Need help with query - joining tables

I am familiar with joining tables, but this particular setup has really gotten to me. I don't know if I'm overthinking it or what. Here is an example of my structure:

Songs
ID | Song_Name

Genres
ID | Genre_Name

Groups
ID | Group_Name

Song_Genres (links songs table to genres table)
Song_ID | Genre_ID

Group_Linking (links groups table to genres table)
Genre_ID | Group_ID


I have genre "groups" where you might combine country and western, or rap and r&b into a similar group to pull together. Each song can be listed in multiple genres, and each genre can be in multiple groups. That's why I need the 2 linking tables.

Now what I am trying to do is pull all songs that are in group 1. It sounds really easy, but I am not able to wrap my head around it yet. I tried the simplest way I could think of, but its duplicating my song for each genre its in.

SELECT songs.id, songs.song_name FROM songs, song_genres, group_linking WHERE songs.id = song_genres.song_id AND song_genres.genre_id = group_linking.genre_id AND group_linking.group_id = 1

I think I need a LEFT JOIN but not sure how to do that with the 3 tables. Any help would be greatly appreciated!
Reply With Quote
  #2 (permalink)  
Old 08-28-11, 19:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by webchris View Post
... but its duplicating my song for each genre its in.
you just need to add DISTINCT to your query

and of course it's a lot easier to write and to understand if you used explicit JOIN syntax rather than the old-style deprecated comma list syntax
Code:
SELECT DISTINCT
       songs.id
     , songs.song_name 
  FROM group_linking
INNER
  JOIN song_genres
    ON song_genres.genre_id = group_linking.genre_id
INNER
  JOIN songs
    ON songs.id = song_genres.song_id
 WHERE group_linking.group_id = 1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-28-11, 19:58
webchris webchris is offline
Registered User
 
Join Date: Aug 2011
Posts: 2
Thanks r937! I have been using JOIN more and more lately, but this was the first case where I needed 3 tables in one query. Thanks again!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On