Join using combined conditions on one join table

Posted by Nathan Wienert on Stack Overflow See other posts from Stack Overflow or by Nathan Wienert
Published on 2012-12-09T20:03:14Z Indexed on 2012/12/09 23:04 UTC
Read the original article Hit count: 137

Filed under:
|
|

I have join a table joining songs to genres. The table has a 'source' column that's used to identify where the genre was found. Genres are found from blogs, artists, tags, and posts.

So,

songs | song_genre                 | genres 
id    | song_id, source, genre_id  | id

What I want to build is a song SELECT query that works something like this, given I already have a genre_id:

IF exists song_genre with source='artist' AND a song_genre with source='blog'
OR exists song_genre with source='artist' AND a song_genre with source='post'
OR exists song_genre with source='tag'

I'm was going to do it by doing a bunch of joins, but am sure I'm not doing it very well.

Using Postgres 9.1.

© Stack Overflow or respective owner

Related posts about sql

Related posts about postgresql