Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Performing a conditional join

Author  Topic 

shawnmolloy
Yak Posting Veteran

93 Posts

Posted - 2007-10-10 : 07:10:34
Hello,

I want to write a query that joins data in a different table based on a column value. The table is for a "Playlist" and holds play list items. The items can be video, audio, images, etc. The playlist table looks like this:

Table_Playlist
-----------------
ID (int)
MediaType (char)
MediaId (int)

Table_Audio
-----------------
MediaId (int)

Table_Video
--------------
MediaId (int)


If the Table_Playlist.[MediaType] column value = "Audio" then I want to join to the Table_Audio table. If the value = "Video" then I need the video table.

Hope that makes sense. Thanks

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-10 : 07:32:55
[code]SELECT tp.*
FROM Table_Playlist AS tp
INNER JOIN (
SELECT MediaID, 'Audio' AS MediaType FROM Table_Audio UNION ALL
SELECT MediaID, 'Video' FROM Table_Audio
) AS q ON q.MediaID = tp.MediaID AND q.MediaType = tp.MediaType[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -