phpmysqldescribe

What is the cleanest way to sort "describe table" query results?


I'm working on "describe table" output to show a list of fields and their types, i want my primary keys to be at top of the list.. I think there's no way to sort describe's results using SQL (something like 'order by') rather than sorting it in PHP.

what do you think guys ? thanks


Solution

  • You're correct. MySQL will always output columns in their actual order — that is, the order they're physically stored in the table's data.

    You can physically move columns around within your table so that the primary keys are first, though this will require MySQL to lock the table and rewrite its entire data in the new order. That's almost never worthwhile.

    If you're just interested in presenting nice output, regardless of how the table is actually structured, you can indeed sort the columns in PHP, perhaps using a simple usort() call.