SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with a query


Help with a query

Author
Message
talben30
talben30
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 2
Hi,

I have a query and I am not sure why it doesnt return any lines. I am building some kind of a bug system and I want to show all the kinds of fields. I have 2 main tables: users, tickets. Now, I made reationship between them and for each reported by I want to assign an name of user by his id in the user table. The same goes for assign to. When I ran this query:
"SELECT UsersTbl.Name AS first, UsersTbl.Name AS second
FROM Tickets INNER JOIN
UsersTbl ON Tickets.Reported_by = UsersTbl.Id INNER JOIN
UsersTbl AS UsersTbl_1 ON Tickets.Assign_to = UsersTbl.Id"
I dont get any thing in return. Why ?
ChrisM@home
ChrisM@home
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2030 Visits: 10354
Hello and welcome to ssc.

Your second INNER JOIN condition references the table alias of the first. This should work, assuming suitable values in your data:

SELECT 
u1.Name AS first,
u2.Name AS second
FROM Tickets t
INNER JOIN UsersTbl u1
ON t.Reported_by = u1.Id
INNER JOIN UsersTbl u2
ON t.Assign_to = u2.Id



If it doesn't return any rows, try this to see what's happening:
SELECT t.*, u1.*, u2.*
FROM Tickets t
LEFT JOIN UsersTbl u1
ON t.Reported_by = u1.Id
LEFT JOIN UsersTbl u2
ON t.Assign_to = u2.Id




Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
talben30
talben30
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 2
It works !!! thanks....
I wrote as usertbl1 - so it will not refer to the first field.
why it is not the same as what you did ?
Any way, you really helped me.
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3685 Visits: 5177
You did not get any results because you probably did not have records in table "Tickets" where "Reported_by = Assign_to"

SELECT   UsersTbl.Name AS first, UsersTbl.Name AS second
FROM Tickets
INNER JOIN UsersTbl ON Tickets.Reported_by = UsersTbl.Id -- Fine, upto this point
INNER JOIN UsersTbl AS UsersTbl_1 ON Tickets.Assign_to = UsersTbl.Id -- Here, you are accessing the table with alias name "UsersTbl_1"

/*Hence, the above query is equivalent to the below query*/
SELECT UsersTbl.Name AS first, UsersTbl.Name AS second
FROM Tickets
INNER JOIN UsersTbl ON Tickets.Reported_by = UsersTbl.Id AND Tickets.Assign_to = UsersTbl.Id -- You can move the condition up like this
INNER JOIN UsersTbl AS UsersTbl_1 ON 1 = 1

/*The above query is again equivalent to below query*/
SELECT UsersTbl.Name AS first, UsersTbl.Name AS second
FROM Tickets
INNER JOIN UsersTbl ON Tickets.Reported_by = UsersTbl.Id AND Tickets.Assign_to = UsersTbl.Id
CROSS JOIN UsersTbl AS UsersTbl_1




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search