13

Is there a way to sort the list of tables returned by mysql's 'show tables' command?

mysql> show tables;

I'd like to sort alphabetically by the table name.

EDIT:

As pointed out by one of the answers, they are already in alphabetical order. However, A != a. Is there a way to ignore case in the sort?

1

4 Answers 4

29

Query information_schema and replace database_name with the name of the database you want to return the tables from

SELECT table_name, engine
FROM information_schema.tables    
WHERE table_type = 'BASE TABLE' AND table_schema='database_name'  
ORDER BY table_name ASC;
5

They are already in alphabetical order!

0
SELECT CONCAT(`table_name`, '') 
FROM information_schema.tables    
order by 1 asc

All you need, just transform the table_name to regular varchar type. And then order it as usual string.

2
  • Why do we need this? What type is TABLE_NAME, numeric? Jun 30, 2011 at 0:34
  • Regards this docs by default case insensitive works only with latin1 and latin1_swedish_ci collations. But the table_name is utf8_general_ci collation. Probably it is a case sensitive.
    – Vagif
    Jun 30, 2011 at 6:25
0

Please try this one and replace database name accordingly.

SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_schema = 'database_name' ORDER BY table_name ASC;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.