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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 simple query... maybe im a dumbass

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 1
user name
user id primary key

table 2
id
user id foreign key
roleid foreign key

in 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 JOIN

select *
from table1 t1 inner join table2 t2
on t1.user_id = t2.user_id



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

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 1
user name
user id primary key

table 2
id
user id foreign key
roleid foreign key

in 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 t1
inner join table2 t2
on t1.user id=t2.user id
group by t1.user id

you can use min() or max() or any other aggregate functions to retrieve t2 fields based on your reqmnt.
Go to Top of Page
   

- Advertisement -