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
 join query

Author  Topic 

esambath
Yak Posting Veteran

89 Posts

Posted - 2008-11-20 : 01:41:40
Hi friends,

Please help.I have 3 Table

KR_category
CatId
Name
Parentid

KR_Request
ReqId
CatId
UserId
ReporterId [userid or 0]
Topic
Description
State

kr_Usermanagement
UserId
Name


I have display the Result {Username, Reporter Name, Category Name, Description, and State} for particular ReqId

This is my query, I need to modify the query

SELECT u1.Name AS UserName ,u2.Name AS ReporterName,c.Name as CategoryName,r.Description,r.State,
r.Abusive,r.Topic,r.Status,r.reqid
FROM kr_Request r JOIN kr_Usermanagement u1 ON u1.UserId=r.UserId
JOIN kr_Usermanagement u2 ON u2.UserId=r.ReporterId
join Kr_Category c On c.catId=r.CatId
where r.Deleted=0 and ReqId=90 Order by r.creationdateTime desc



The scenario is when the Reporter Id is zero, there is no userid will be found, so it will doest not return any value.



But what I need is when the reporterid is zero the reporter name should be display like “No Reporter”….

If the reporter id is equal to the user id, that display the actual results…


Thanks and Regards
E.sambath kumar

Deon Smit
Starting Member

47 Posts

Posted - 2008-11-20 : 01:46:40
Try using a outer join or left outer join. Had the same thing a while back. It will not loose any records then.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-20 : 01:49:13
[code]SELECT u1.Name AS UserName ,COALESCE(u2.Name,'No Reporter') AS ReporterName,c.Name as CategoryName,r.Description,r.State,
r.Abusive,r.Topic,r.Status,r.reqid
FROM kr_Request r JOIN kr_Usermanagement u1 ON u1.UserId=r.UserId
LEFT JOIN kr_Usermanagement u2 ON u2.UserId=r.ReporterId
join Kr_Category c On c.catId=r.CatId
where r.Deleted=0 and ReqId=90
Order by r.creationdateTime desc
[/code]
Go to Top of Page
   

- Advertisement -