|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I have 5 tables in a database(below).
Each User is in a group, each group has a set of permissible operations. If I have a UserID, how can I get a list of permissible "OperationName"s? Code:
GroupAssign +---------+--------+ | GroupID | UserID | +---------+--------+ | 1 | 1 | | 2 | 1 | | 2 | 2 | +---------+--------+ GroupInfo +---------+-----------+ | GroupID | GroupName | +---------+-----------+ | 1 | Student | | 2 | Teacher | +---------+-----------+ OperationAssign +---------+-------------+ | GroupID | OperationID | +---------+-------------+ | 2 | 2 | | 2 | 1 | | 1 | 1 | +---------+-------------+ OperationInfo +-------------+---------------+ | OperationID | OperationName | +-------------+---------------+ | 1 | View | | 2 | Edit | +-------------+---------------+ UserInfo +--------+----------+ | UserID | UserName | +--------+----------- | 1 | Sean | | 2 | Bob | +--------+----------+ |
|
#2
|
||||
|
||||
|
it's a straightforward join
Code:
select GA.UserID
, GA.GroupID
, OA.OperationID
, OI.OperationName
from GroupAssign GA
inner
join OperationAssign OA
on GA.GroupID = OA.GroupID
inner
join OperationInfo OI
on OA.OperationID = OI.OperationID
order
by GA.UserID
, GA.GroupID
UserID,GroupID,OperationID,OperationName
1,1,1,View
1,2,1,View
1,2,2,Edit
2,2,2,Edit
2,2,1,View
obviously, this data shows that a user can belong to more than one group, and a group can have more than one operation thus, we need DISTINCT as well for a specific UserID, add a WHERE clause Code:
select distinct
GA.UserID
, OI.OperationName
from GroupAssign GA
inner
join OperationAssign OA
on GA.GroupID = OA.GroupID
inner
join OperationInfo OI
on OA.OperationID = OI.OperationID
where GA.UserID = 1
order
by GA.UserID
, OI.OperationName
UserID,OperationName
1,Edit
1,View
|
|
#3
|
|||
|
|||
|
Thats perfect. Thanks a lot for the help.
Shane Blazek |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Help w/ selecting data in multiple tables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|