Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Select DI.*, C.* from  Table1 DI LEFT join Table2 C on dI.Key=C.Key
								  ANd (Di.Status <16  OR Di.Status =36 OR DI.Status =40) AND (DI.UserID=@userID OR C.UserID=@UserID)
								  where  DI.Department = @Department


i want the result from Both Table1, Table2 where UserID=@UserID

What I have tried:

the above Query Shows the result from Table1 where userID not Equal to @UserID i.e For Key 101 Record not Exist in Table2 and UserID not Equal To @UserID
Posted
Updated 31-Oct-19 0:36am
v4

You have to tell the select clause what table.columns (using the specified aliases) that you want data from.
SQL
SELECT DI.*, C.* FROM...
 
Share this answer
 
Not quite sure what you are asking but one of two things I think.

Guessing you don't want records from Table1 if there is no matching record in Table2 and Table1.UserID <> @UserID

Really easy move the second part of the ON clause down into the WHERE clause, the NULL Table2.UserID null rows will cause failed tests but because you are using OR that's actually what you want.

Now guessing you want to know a load of stuff about Table1 users and then also record Table2 users who are not in Table1.

That's a bit harder but not much. You could use a cross join but you'd need to move the ON clause to the WHERE clause and handle NULLS on your test on department, so I'd use a FULL OUTER JOIN - see below.

Select DI.*, C.* from Table1 DI full join Table2 C on dI.[Key]=C.[Key]
ANd (Di.[Status] <16 OR Di.[Status] =36 OR DI.[Status] =40)
AND DI.Department = @Department AND (DI.UserID=@userID OR C.UserID=@UserID)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900