开发者

SQL order by a column from another table

I have 3 tables: people, groups and memberships. Memberships is a join table between people and groups, and have 3 columns: personId, groupId and description (te开发者_开发知识库xt).

I want to select entries from the memberships table depending on a groupId but sorting the result by the names of people associated to the found memberships (name is a column of people table)

SELECT * FROM "memberships" WHERE ("memberships".groupId = 32) ORDER BY (?????)

Is it possible to achieve this in one single query?


Join to the people table and then order by the field that you want.

SELECT
  m.* 
FROM 
  "memberships" AS m
  JOIN "people" AS p on p.personid = m.personID
WHERE
  m.groupId = 32
ORDER BY 
  p.name


SELECT *
FROM Membership AS m
     JOIN People as p ON p.personID = m.personID
WHERE m.groupID = 32
ORDER BY p.name


SELECT
      M.* ,
      P.Name AS PersonName
FROM 
      Memberships AS m
INNER  JOIN 
      People AS P ON P.PersonID = M.PersonID
WHERE
      M.GroupID = 32
ORDER BY 
      PersonName
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜