Click here to Skip to main content
15,907,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

This should have been easy but I don't know what I am missing here.
I have to join tables like this:

table 1:
empid empname mngrid
emp1 emp1name mn1
emp2 emp2name mn1
emp3 emp3name mn1

table 2:
empid mngrid score
emp1 mn1 3
emp2 mn1 2

I need rows from table 1 which are present in table 2 and also which are not based on condition as:
empname, score
emp1name, 3
emp2name, 2
emp3name, null

What I have tried:

SELECT EMPNAME, SCORE
FROM TABLE1 UD, TABLE2 US
WHERE UD.MNGRID=US.MNGRID
AND US.MNGRID='mn1'


but this has duplicate data.
Posted
Updated 21-Jul-16 1:32am

1 solution

Try:
C#
SELECT a.empname, b.score
FROM Table1 a
LEFT JOIN Table2 b ON a.empid = b.empid
 
Share this answer
 
Comments
planetz 21-Jul-16 7:37am    
There are multiple conditions. I want the data where manager is mn1 also.
OriginalGriff 21-Jul-16 7:40am    
So add it...
LEFT JOIN Table2 b ON a.empid = b.empid
WHERE a.mngrid='mn1'
planetz 21-Jul-16 7:45am    
It worked now!!! Thank you...!! Don't know why there was error before. Must be something I missed..!!
OriginalGriff 21-Jul-16 8:23am    
It's the LEFT JOIN that does it, rather than a SELECT ... FROM with multiple tables.
planetz 22-Jul-16 1:33am    
Thank you so much...!!

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