I have already built a table with field names in arbitrary order. I want those field names to be in alphabetical order so that I can use them in my dropdown list. Is it possible with a query?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Note: The following code will alter the specified table and reorder the columns in alphabetical order
This should do the trick. It’s a bit messy and lengthy, and you’ll have to change the database name and table name, but for this one, the only requirement is that there is a database named “test” and that you are running these commands in it:
Let’s create the tables we need:
Now let’s create the two stored procedures required for this to work:
Separating them since one will be responsible for loading the commands, and including a cursor to immediately work with it isn’t plausible (at least for me and my mysql version):
If anyone thinks/sees that I’m missing to include any important column attributes in the
ALTERcommand, please hesitate not and mention it! Now to the next procedure. This one just executes the commands following the order of columnidfrom theloadcommandstable. :The SQL is long, so if someone can point out ways (and has tested them) to make this shorter I’d gladly do it, but for now, this at least works on my end. I also didn’t need to put dummy data in the
alphabettable. Checking the results can be done using theSHOW...command.The last part:
Perhaps later on I could make
reorder_loadcommandsdynamic and accept table and schema parameters, but I guess this is all for now..