2016-10-17 3 views
0

Ich bin mit unter Logik in SQL-Server stecken.Joining zwei Tabellen und doppelte Zeilen vermeiden

Tabelle 1:

ID Requestid 
1 0001  
2 0004  
3 0004   
1 0005 

Tabelle 2

parentID Requestid Age 
1   0001  29 
2   0004  30 
3   0004  34 
1   0005  27 

query:

select * from table1 t1 
join table t2 
on t2.parentid =t1.id 

Wenn ich diese Tabellen beitreten, ich bin unten Ergebnis zu erzielen

ID  requestid   age 
1   0001    29 
1   0005    29 
2   0004    30 
3   0004    34 
1   0001    27 
1   0005    27 

Ich möchte unten Ergebnis:

ID  requestid   age 
1   0001    29 
1   0005    27 
2   0004    30 
3   0004    34 

Ich weiß, es ist einfach und ich bin etwas fehlt. Jede Hilfe wird geschätzt!

Antwort

0
select ID, requestid, age from table1 t1 
inner join table t2 
on t2.parentid =t1.id AND t2.requestId = t1.requestId 
ORDER BY ID 

ODER

select ID, requestid, age from table1 t1,table t2  
where t2.parentid =t1.id AND t2.requestId = t1.requestId 
ORDER BY ID 
+0

perfekt! Ich verpasste die Reihenfolge nach ID und schloss mich zwei anderen Spalten an. – chits