Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7829161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:38:47+00:00 2026-06-02T10:38:47+00:00

What is the best way to sort a result set in MySQL based on

  • 0

What is the best way to sort a result set in MySQL based on a field value that could come from multiple tables?

For the sake of simplicity, assume there are four tables in the DB. The primary table contains a number of fields, but particularly it contains a column with the name of another table and a foreign key to a record in said table. Each of the three foreign tables contain a name column that I would like to sort the results by in the primary table.

Example primary table:

+----+---------------+------------+-------------+
| id | foreign_table | foreign_id | more_fields |
+----+---------------+------------+-------------+
|  1 | students      |       9182 | blah, blah  |
|  2 | students      |       1008 | blah, blah  |
|  3 | parents       |       3827 | blah, blah  |
|  4 | teachers      |       4523 | blah, blah  | 
|  5 | teachers      |       1092 | blah, blah  | 
+----+---------------+------------+-------------+

Example foreign (students) table:

+-------+--------------+-------------+
| id    | name         | more_fields |
+-------+--------------+-------------+
|  9182 | Joe          |  blah, blah | 
|  1008 | Sally        |  blah, blah | 
+-------+--------------+-------------+

Is this possible in MySQL? Or do I need to loop through the results in PHP and create an array to sort by and then use something like array_multisort()? If using an array, would it be efficient to loop through thousands of rows just to sort the results?

Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:38:48+00:00Added an answer on June 2, 2026 at 10:38 am

    Using three LEFT JOINs (students, parents, teachers) and listing each in the ORDER BY ought to do the job if you COALESCE() the names from the other three tables together:

    SELECT
      primary_table.*,
      COALESCE(students.name, parents.name, teachers.name) AS name
    FROM 
      primary_table
      LEFT JOIN students ON (foreign_table = 'students' AND foreign_id = students.id)
      LEFT JOIN parents ON (foreign_table = 'parents' AND foreign_id = parents.id)
      LEFT JOIN teachers ON (foreign_table = 'teachers' AND foreign_id = teachers.id)
    ORDER BY name 
    

    In the long run, I would suggest changing this schema to store all of the different types of names in one table, with an identifier in that table indicating whether it is a student, teacher, or parent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the best way to set up a controller to sort by many (possibly
What is the best way to sort the results of a sql query into
What is the best/easiest way to sort a large list of words (10,000-20,000) by
I can't figure out what would be the best way to use Bucket Sort
Greetings, Trying to sort through the best way to provide access to my Entity
The best way to store images into MySQL is by storing the image location
Which is the best way to implement factory method. 1) A factory method that
I'm looking for sort of a 'best practice' way to tackle this common scenario.
the best way to explain is with example so: this is the model public
The best way I can think of to ask this is by example... In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.