来自另一个表的列的SQL顺序

时间:2009-11-26 20:54:39

标签: sql sql-order-by

我有3张桌子:人员,团体和会员资格。成员资格是人与群体之间的联接表,有3列:personId,groupId和description(text)。

我想根据groupId从成员资格表中选择条目,但是按照找到的成员资格相关人员的姓名对结果进行排序(名称是人员表的列)

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

是否可以在一个查询中实现此目的?

3 个答案:

答案 0 :(得分:29)

加入人员表,然后按照您想要的字段排序。

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

答案 1 :(得分:3)

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

答案 2 :(得分:1)

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