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.
Author |
Topic |
thenamenoonehastaken
Starting Member
3 Posts |
Posted - 2008-01-17 : 00:59:02
|
Hi,Im new to SQL... and what im trying to achieve is a query that extracts data from one table that contains multiple records that are mapped to a single record.table 1user nameuser id primary keytable 2iduser id foreign keyroleid foreign keyin table 2 the user can have multiple records associated to the record in table 1. how would i go about retrieving 1 record from table 2 that is associated to table 1 ?any assistance greatly appreciated.thanks |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-01-17 : 01:19:13
|
use INNER JOINselect *from table1 t1 inner join table2 t2on t1.user_id = t2.user_id KH[spoiler]Time is always against us[/spoiler] |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-17 : 04:33:10
|
quote: Originally posted by thenamenoonehastaken Hi,Im new to SQL... and what im trying to achieve is a query that extracts data from one table that contains multiple records that are mapped to a single record.table 1user nameuser id primary keytable 2iduser id foreign keyroleid foreign keyin table 2 the user can have multiple records associated to the record in table 1. how would i go about retrieving 1 record from table 2 that is associated to table 1 ?any assistance greatly appreciated.thanks
select t1.[user id],min(t2.roleid)from table1 t1inner join table2 t2on t1.user id=t2.user idgroup by t1.user idyou can use min() or max() or any other aggregate functions to retrieve t2 fields based on your reqmnt. |
 |
|
|
|
|