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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 joining 1 column onto 2 different tables

Author  Topic 

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2009-10-13 : 07:19:06
Hi,

Not sure if this is possible, I have a query as shown below.

Basically the column "postedBy_userID" contains an ID column but it can be associated with 1 of 2 tables. The column "postedBytypeID" denotes which table the "postedBy_userID" will be joined on to.

I want to write this query, and have it join onto either

tblUsers1 or tblUsers2 via the column userID.

Does this look like a bad design? I could create seperate tables for replies from either tblUsers1 and tblUsers2.

I am not sure how I would programatically join.

Does anyone have any suggestions?

Thanks!
mike123



SELECT replyCode,O_R.commentsInstructions,O_R.commentDate,O_R.postedBy_userID,O_R.postedBytypeID,O_R.readByRecipient

FROM tblOrder_Replies O_R

JOIN tblOrders O on O_R.orderID = O.orderID

WHERE O_R.orderID = @orderID and O.clientID = @clientID AND O_R.approvedByAdmin = 1

ORDER BY O_R.commentDate DESC

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-10-13 : 07:31:27
Can you show the structure of tblUsers1 and tblUsers2 and the wanted fields in your select list?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-10-13 : 08:04:37
A simple UNION in a derived table will do...


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2009-10-13 : 08:09:22
Hey Fred,

It's really simple actually. All I want to include is their name, which is the column "nameOnline" on both tables. The tables just denote clients and wholesalers. There are other columns too for these tables but they don't need to be brought back, and I have excluded them from the statements to make this post more simple.

Your help is much appreciated!

Thanks again! :D
mike123




CREATE TABLE [dbo].[tblUsersC](
[c_userID] [int] IDENTITY(1,1) NOT NULL,
[firstName] [varchar](50) NOT NULL
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[tblUsersW](
[w_userID] [int] IDENTITY(1,1) NOT NULL,
[firstName] [varchar](50) NOT NULL
) ON [PRIMARY]

GO
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-10-13 : 08:14:47
SELECT *
FROM MasterTable AS mt
INNER JOIN (SELECT ID, Name, 'c' AS Type FROM Table1 UNION ALL SELECT ID, Name, 'e' AS Type FROM Table2) AS d
ON d.ID = mt.ID AND d.Type = mt.Type



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2009-10-13 : 08:28:37
Hey Peso,

This looks great and looks like exactly what I will need, awesome! :)

I am wondering tho, do you think my design is flawed ? I could have a seperate table for each user (only 2 types) and avoid this UNION statement. it would require me to break out a few SP's into their own dedicated versions. I am considering doing breaking them out, but undecided.

Or does this strictly boil down to user preference?

Thanks again!
mike123
Go to Top of Page
   

- Advertisement -