SQL 按另一表中的列排序 [英] SQL order by a column from another table

查看:18
本文介绍了SQL 按另一表中的列排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个表:人员、组和成员资格.Memberships 是人与组之间的连接表,有 3 列:personId、groupId 和 description(文本).

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 (text).

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

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?

推荐答案

加入 people 表,然后按所需字段排序.

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

这篇关于SQL 按另一表中的列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆