tags:

views:

244

answers:

1

Hey guys. Simple one this, but one I can't seem to find any information on so here goes.

I need to find the columns in a specific table, which is no problem....

SHOW COLUMNS FROM tablename LIKE '%ColumnPrefix%';

But I need to know what order they will be returned, preferable by choosing to order the results ascending alphabetically. I have had no luck with using ORDER BY Field.
Any ideas?

Cheers!

+4  A: 

You can query the table INFORMATION_SCHEMA.COLUMNS to get the information that SHOW COLUMNS gives you, plus it allows you to use ORDER BY or any other SQL syntax you might want to use:

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tablename'
  AND column_name LIKE 'ColumnPrefix%'
ORDER BY column_name
Mark Byers
+1 Good point - the INFORMATION_SCHEMA database (since 5.0) is probably the most overlooked useful thing in MySQL.
Piskvor
Am running version 4.0.16 otherwise that would be a great way of doing the job.
rich