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.
Author |
Topic |
rama108
Posting Yak Master
115 Posts |
Posted - 2013-10-24 : 18:00:34
|
I have a query with the following join, is this correct?LEFT JOIN admin.tblUser uON (c.UpdatedByUserID = u.UserIDOR c.UpdatedByUserID = u.UserID)Thanks. |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-10-24 : 18:43:59
|
quote: Originally posted by rama108 I have a query with the following join, is this correct?LEFT JOIN admin.tblUser uON (c.UpdatedByUserID = u.UserIDOR c.UpdatedByUserID = u.UserID)Thanks.
Syntactically it appears correct. But, it doesn't make much sense. |
|
|
rama108
Posting Yak Master
115 Posts |
Posted - 2013-10-24 : 19:00:09
|
Lamprey, Thank you for your response. Here is the full query: Does this makes sense?SELECT c.ID,p.FirstName + ' ' + p.LastName UploadedByp.FirstName + ' ' + p.LastName UpdatedByFROM Categories cLEFT JOIN admin.tblUser uON (d.UploadedByUserID = u.UserIDOR d.UpdatedByUserID = u.UserID)LEFT JOIN person.tblPerson pON u.PersonID = p.PersonID |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-10-24 : 20:07:57
|
Ahh, I see. You probably miss-typed UpdatedByUserID twice in the original post. Yeah, I think that makes sense. I'm guessing the table alias of "d" should be "c". But, if the joining UserID can be in either of those two columns on table "d," then it looks correct. |
|
|
rama108
Posting Yak Master
115 Posts |
Posted - 2013-10-24 : 21:13:15
|
Thank you for verifying, Thanks for your time. |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2013-10-25 : 11:20:38
|
Rama, the second query you posted has some fatal flaws (no table with a d alias, no comma after UploadedBy, no way to differentiate UploadedBy vs UpdatedBy), but I wanted to point out that ORs in the JOIN criteria and/or WHERE clause can be a big performance killer. This is because it can prevent proper index utilization. It's going to depend on your data volume, index definition and environment, but try union:SELECT c.ID,'UploadedBy' AS NameType,p.FirstName + ' ' + p.LastName AS FullNameFROM Categories cJOIN admin.tblUser uON d.UploadedByUserID = u.UserID -- Where is the d alias coming from?JOIN person.tblPerson pON u.PersonID = p.PersonIDUNION ALLSELECT c.ID,'UpdatedBy' AS NameType,p.FirstName + ' ' + p.LastName AS FullNameFROM Categories cJOIN admin.tblUser uON d.UpdatedByUserID = u.UserID -- Where is the d alias coming from?JOIN person.tblPerson pON u.PersonID = p.PersonID |
|
|
rama108
Posting Yak Master
115 Posts |
Posted - 2013-10-25 : 21:17:50
|
Lazerath, Finally I corrected the query a few hours before you posted the message. Thank you also for pointing it out. |
|
|
|
|
|
|
|