views:

57

answers:

1

I have three tables specifying important columns below

  1. Users(Id, username)
  2. Groups(Id, groupname, creator) (creator is the creator of the group)
  3. Association(Id, UserId, GroupId) (entries in this table include which user is in which group)

Association.UserID =Users.Id, Association.GroupId = Groups.id and also Groups.creator = Users.Id.

What I require are atleast three columns displaying all the data

  1. Groupname
  2. Group Creator (the user)
  3. Associated user to the group (this info is in table association)
+2  A: 
SELECT G.GroupName, U1.UserName AS GroupCreator U2.UserName AS GroupMember
FROM Groups AS G
INNER JOIN Users AS U1
    ON G.Creator = U1.ID
LEFT JOIN Association AS A
    ON A.GroupID = G.ID
LEFT JOIN Users AS U2
    ON U2.ID = A.UserID
TcKs