m:n关系中的select语句

时间:2013-03-20 13:53:07

标签: tsql select many-to-many

我有三张桌子:

USER
用户ID
FNAME
L-NAME

GROUP
GROUPID
描述

USER_GROUP
usergroupid
用户ID
GROUPID

我需要获取users表的名字和姓氏。给出的是组描述。我怎么能实现这个目标?

3 个答案:

答案 0 :(得分:1)

SELECT  a.Fname,
        a.LName,
        c.Description
FROM    [USER] a
        INNER JOIN [USER_GROUP] b
            ON a.userID = b.userID
        INNER JOIN [GROUP] c
            ON b.groupid = c.groupID
WHERE   c.Description = 'description_here'

要进一步了解联接,请访问以下链接:

答案 1 :(得分:1)

试试这个:

SELECT USER.*
FROM USER 
  INNER JOIN USER_GROUP ON USER.userid = USER_GROUP.userid
  INNER JOIN [GROUP] ON USER_[GROUP].groupid = [GROUP].groupid
WHERE [GROUP].description = 'Blah blah'

答案 2 :(得分:1)

怎么样

   Select FName, LName
   From user u
   Where Exists 
       (Select * From user_Group ug
            join group g On g.GroupId = ug.groupId
        Where ug.userId = u.UserId
            anf g.description = @GroupDescription)