Reputation: 3513
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).
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?
Upvotes: 23
Views: 53637
Reputation: 100567
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
Upvotes: 2
Reputation: 22187
SELECT *
FROM Membership AS m
JOIN People as p ON p.personID = m.personID
WHERE m.groupID = 32
ORDER BY p.name
Upvotes: 3
Reputation: 46913
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
Upvotes: 38