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 7185425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:24:46+00:00 2026-05-28T18:24:46+00:00

I have a row of entities stored in a mysql table; entity table order

  • 0

I have a row of entities stored in a mysql table;

entity table

order
id
name 
type (refers to Type(id))

and a row of types stored in another table

Type table

 id
 name
 order

the ORDER column in Type table specifies by what order the entities should be sorted – some should be ordered by name and some by id.

How do I create a mysql query that gets the ORDER BY clause from the type table and sorts the entities in the entity table by the ORDER stored for that entity type in the Type table

for example, let us say I have the following rows:

Entity table:

row 1:

id = 1
name = Virginia
type = 1

row 2:

id = 2
name  = Virginia
type = 1

row 3:

id = 3
name  = Canada
type = 2

types (rows in Type table)

row 1

id = 1
name = states
order = "name"

row 2:

id = 2
name = countries
order =  id

I want to do the following query

SELECT entities.id, entities.name FROM entities INNER JOIN type ON entities.type = type.id ORDER BY ....

in the ORDER BY I want to order the entities based on what is stored in the ORDER row in the type table. So countries should be sorted by Entity(ID) and states should be sorted by Entity(name). How can I do that?

  • 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-05-28T18:24:47+00:00Added an answer on May 28, 2026 at 6:24 pm

    This doesn’t seem like a very good design for a database. For your example, I would suggest something more similar to this:

    CREATE TABLE countries (
        countryID INT NOT NULL,
        countryName VARCHAR(30)
    );
    
    CREATE TABLE states (
        stateID INT NOT NULL,
        countryID INT,
        stateName VARCHAR(30)
    );
    

    Then you can perform queries like:

    SELECT c.countryName, s.stateName
    FROM countries c LEFT JOIN states s ON c.countryID = s.countryID
    ORDER BY countryName, stateName;
    

    At the very least, I would suggest using more obvious names for your columns, like in your entity table, you have a column named ‘type’ which refers to the ‘id’ field in the type table. Perhaps name them both typeID or something more obvious.

    I also think it’s a bad idea to create a column that stores information about which column to order by. Not only does that mean that you’ll have to execute two queries every time (one to fetch the order by, and one to fetch the actual data), but you will also be storing a lot of extra data unnecessarily.

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

Sidebar

Related Questions

I have two entities, Customer and Order, both of which I have created types
let's say that I have a row in my table: | ID | Name
I have 3 entities inheriting from another entity. I'm using the strategy Single_Table @Inheritance(strategy
I have 2 tables. 1 has entity's, 1 per row. Another is simply a
I have the following core data model with two entities: entity item which holds
I have a table fill query, which have to get all references entities. I
I have a entity Player that has one to many association to other entities.
I have a question about Entity Framework and Linq to Entities. My .NET version
I have two entities, User and UserPermission. The User entity contains all your normal
I have a situation where I'm adding existing entities to an entity collection. Before

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.