MySQL table names ordered by dependency order

It sounds like you're trying to dump tables that have foreign key definitions, and you want to make sure the "parent" tables are dumped first so the "child" tables that reference them can be assured their foreign key definitions will work.

You can't do this in general, because cyclic dependencies are possible.

For example, if you have users and teams where each user has a reference to the team they belong to, but teams also has a reference captain to the specific user who is the team's captain, do you want to list users first, or teams first?

One alternative solution is to output all the tables in any order you want, but without foreign key definitions. After all the tables have been listed and their data too, then follow that with ALTER TABLE...ADD FOREIGN KEY commands.

Another alternative solution—the one used by mysqldump—is to SET FOREIGN_KEY_CHECKS=0 at the beginning. Then you can define the foreign key constraints without worrying about whether the referenced table has been created yet. The tables are dumped in alphabetical order.

But to answer your question more directly: you can use the INFORMATION_SCHEMA to discover which table dependencies exist.

SELECT table_schema, table_name, 
  GROUP_CONCAT(column_name ORDER BY ordinal_position) AS `columns`,
  MAX(referenced_table_schema) AS referenced_table_schema,
  MAX(referenced_table_name) AS referenced_table_name,
  GROUP_CONCAT(referenced_column_name ORDER BY ordinal_position) AS `ref_columns`
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE referenced_table_schema IS NOT NULL
GROUP BY table_schema, table_name;

MySQL doesn't support recursive queries until MySQL 8.0 (which is still under development). So you'll probably have to fetch the dependency data into your application and figure out the order you want to dump them.

But you still won't be able to handle cycles this way. You'll have to use one of the alternatives I described above.