Click here to Skip to main content
15,923,087 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello friens,
i need help with join in sql. i have 2 table 1st is tbl_register amd 2nd is request.

Tbl_register

UserID 	   f_name	   Designationnm	Comapny_name
1	   aaa	               CEO	           Apple
2	   Rajeev	   General manager	   Dell
3	   upendra	   Managing director	    Dell
4	   soumendr        Vice president	   lenovo
9	   Suneal	  Managing director	   Apple
8	    asa	               CEO	           Apple

request table

req_from_id	req_to_id	req_status
2	           9	           0
3	           9	           0
4	           9	           0
2	           8	           0
2	           1	           0



i need to get output like this.

f_name       Designationnm   Comapny_name  UserID    req_from_id
aaa  aa             CEO            Apple     1                 2
Rajeev          General manager    Dell      2                 9
Suneal          Managing director  Apple     9             2
asa               CEO              Apple     8             2


What I have tried:

 select distinct 
tb1.UserID,tb1.f_name,tb1.Designationnm,tb1.Comapny_name,tb2.req_from_id	  
from Tbl_register tb1 
left join
tbl_friend_and_match_request tb2
on 
tb1.UserID = tb2.req_from_id
where 
Comapny_name like '%a%' or Designationnm like '%Manager%'
Posted
Updated 30-Aug-17 1:04am
v5
Comments
Rajiv.net40 30-Aug-17 2:16am    
it works but gives duplicate records
Graeme_Grant 30-Aug-17 3:52am    
Please click on the "Improve question" widget above and post the update there - it won't be easily seen in the comments section...
Rajiv.net40 30-Aug-17 3:57am    
have u any solution for my question ?

It gives duplicates, because that is what you told it to do.
If you change it so that it returns req_to_id instead of req_from_id then it's obvious why:
SQL
SELECT r.f_name, r.Designationnm, r.Comapny_name, r.UserID, f.req_to_id
  FROM tbl_Register r
  LEFT JOIN tbl_friend_and_match_request f ON f.req_from_id = r.UserID 
where Comapny_name like '%a%' or Designationnm like '%Manager%' 
Becuase there are three rows in the request table that JOIN with the one row in the register table.
And there is no way that SQL on it's own has any idea which one you want!
f_name	Designationnm	Comapny_name	UserID	req_to_id
aaa       	CEO                 	Apple     	1	NULL
Rajeev    	General manager     	Dell      	2	9
Rajeev    	General manager     	Dell      	2	8
Rajeev    	General manager     	Dell      	2	1
Suneal    	Managing director   	Apple     	9	NULL
asa       	CEO                 	Apple     	8	NULL
Your tables need rethinking, or you need to decide how SQL should "pick" one of the options when this occurs.
 
Share this answer
 
Comments
Rajiv.net40 30-Aug-17 4:27am    
I changed it to req_to_id but it still shows all the entries

aaa CEO Apple 1 1
Rajeev General manager Dell 2 NULL
Suneal Managing director Apple 9 9
Suneal Managing director Apple 9 9
Suneal Managing director Apple 9 9
asa CEO Apple 8 8
OriginalGriff 30-Aug-17 4:31am    
Yes - that's what I said. You need to decide how SQL should select the "one" that you want - we have no idea how it should be picked, and nor does SQL!
Rajiv.net40 30-Aug-17 4:35am    
i dont know how to decide to select one.if u know then tell me or any idea
OriginalGriff 30-Aug-17 5:11am    
It's your data! If *you* don't know on what criteria one should be selected, how do you expect me (or SQL) to have any idea? :laugh:

What makes "9" more appropriate than "8" or "1"? You at least have an idea what this information is need for - we don't.
Rajiv.net40 30-Aug-17 5:16am    
i need all users with friend request id in search result from both table.
Select distinct
tb1.UserID,tb1.f_name,tb1.Designationnm,tb1.Comapny_name,MAX(tb2.req_from_id)
from Tbl_register tb1
left join
tbl_friend_and_match_request tb2
on
tb1.UserID = tb2.req_from_id
where
Comapny_name like '%a%' or Designationnm like '%Manager%'
Group By tb1.UserID,tb1.f_name,tb1.Designationnm,tb1.Comapny_name
 
Share this answer
 
Comments
Rajiv.net40 1-Sep-17 7:09am    
not worked your query.

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