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?