SQL Order by Third Table

i Experts,

In my application, I have agents that are associated with multiple of locations, so my database is as follows (in bold are my table names, followed by field names):

locations
id
name
address

agents
id
name
phone

location_agent_assoc
location_id
agent_id

So my last table is a linked table.

For each location, I need to retrieve a comma-separated list of agent ids associated with that location, so I am using the following code:

SELECT locs.id, name, default_time, default_vehicle, GROUP_CONCAT(DISTINCT assoc.agent_id) AS agent_ids 

FROM locations AS locs 
	INNER JOIN location_agent_assoc AS assoc ON locs.id = assoc.location_id 
	INNER JOIN agents ON assoc.agent_id = agents.id

WHERE locs.archived = 0 AND agents.archived = 0 
GROUP BY locs.id, name, default_time, default_vehicle 

ORDER BY name, agents.fName, agents.lName

Open in new window



As you can see I am trying to order first by location name (works), then I would like to order the agent id list by first name, last name, but this does not give me the correct order of ids. What am I doing wrong?

Thank you
APD TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
GROUP_CONCAT() supports an inline ORDER BY clause,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dsackerContract ERP Admin/ConsultantCommented:
As you can see I am trying to order first by location name (works), then I would like to order the agent id list by first name, last name, but this does not give me the correct order of ids. What am I doing wrong?
Does "but this does not give me the correct order of ids" mean that you expect the ids to be in numerical order at the same time that the first/last names are in alphabetical order?

That probably cannot happen. Those ids are usually set when a person is added into a table, without regard to the order of listing their names.

If I don't understand that correctly, please explain further what you mean.
APD TorontoSoftware DeveloperAuthor Commented:
Cool, Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.