Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 18th, 2003, 05:54 AM
izz izz is offline
Registered User
 
Join Date: Aug 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to izz Send a message via MSN to izz Send a message via Yahoo to izz
Default How to relate a double indexed table?

I have three tables:
Favorates, table1, table2

Favorates contain (UserName, ItemID, TypeID)
Table1 and table1 contain the detailed information about (ItemID) in favorates depending on it's type. If ItemId type is 1 then details are in table1, if ItemID type is 2 Then details are in table2.

Favoraes table key is (UserName+ItemID), i.e, one uername cannot have the same item more than once.

How can I create the relation between favorates table and the other two? ItemID value is always either a key of table one or table 2??

I would be thankfull to whoever help,,,

If you don't have the one you love, love the one you have
 
Old August 18th, 2003, 06:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Use an OUTER JOIN to combine both tables with the Favorites table:
Code:
SELECT UserName, ItemID, CASE 
            WHEN TypeID=1 THEN Table1.Details
            WHEN TypeID=2 THEN Table2.Details
            END as TheDetails
    FROM Favorites
        LEFT JOIN Table1 ON Favorites.ItemID=Table1.ItemID
        LEFT JOIN Table2 ON Favorites.ItemID=Table2.ItemID;

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to relate tables in ASP .NET busybee BOOK: ASP.NET Website Programming Problem-Design-Solution 1 April 2nd, 2006 04:36 PM
How to Relate Tables in ASP .NET busybee ASP.NET 1.0 and 1.1 Basics 4 March 29th, 2006 01:25 AM
"validator" mis-indexed? jemptymethod BOOK: Professional Java Development with the Spring Framework 1 August 1st, 2005 09:17 AM
Need Help : Indexed View upermadi SQL Server 2000 3 September 29th, 2004 06:51 AM
Indexed View jemacc SQL Server 2000 2 July 21st, 2004 05:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.