Reputation: 6339
(Basic question that probably is a duplicate, but I don't know what to search for, so feel free to edit this question with the proper database terms)
How would one retrieve groups of rows (family members
) from a database table based on columns (last
and first
names) efficiently? E.g. from this
last first ...
doe john ...
doe jane ...
smith jimmy ...
smith ted ...
smith anna ...
to something like this (additional data omitted)
doe : [{first:john, ...}, {jane}],
smith: [{jimmy}, {ted}, {anna}]
Does this require retrieving the common data (last name) with distinct
or group by
first and then iterating with additional queries (where last="smith"
) for each name?
I'd think that that naive approach likely is inefficient and there are better solutions.
Upvotes: 1
Views: 38