Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6987925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:00:08+00:00 2026-05-27T19:00:08+00:00

I have already built a table with field names in arbitrary order. I want

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:00:09+00:00Added an answer on May 27, 2026 at 7:00 pm

    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:

    -- CREATE TESTING TABLE IN A DATABASE NAMED "test"
    DROP TABLE IF EXISTS alphabet;
    CREATE TABLE alphabet (
          d varchar(10) default 'dee' not null
        , f varchar(21)
        , e tinyint
        , b int NOT NULL
        , a varchar(1)
        , c int default '3'
    );
    
    -- USE A COMMAND STORAGE TABLE
    DROP TABLE IF EXISTS loadcommands;
    CREATE TABLE loadcommands (
          id INT NOT NULL AUTO_INCREMENT
        , sqlcmd VARCHAR(1000)
        , PRIMARY KEY (id)
    );
    

    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):

    -- PROCEDURE TO LOAD COMMANDS FOR REORDERING
    DELIMITER //
    CREATE PROCEDURE reorder_loadcommands ()
    BEGIN
        DECLARE limitoffset INT;
        SET @rank = 0;
        SET @rankmain = 0;
        SET @rankalter = 0;
        SELECT COUNT(column_name) INTO limitoffset 
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE table_schema = 'test'
            AND table_name = 'alphabet';
    
        INSERT INTO loadcommands (sqlcmd)
        SELECT CONCAT(t1.cmd, t2.position) AS commander FROM (
            SELECT @rankalter:=@rankalter+1 AS rankalter, CONCAT('ALTER TABLE '
                , table_name, ' '
                , 'MODIFY COLUMN ', column_name, ' '
                , column_type, ' '
                , CASE 
                    WHEN character_set_name IS NOT NULL 
                        THEN CONCAT('CHARACTER SET ', character_set_name, ' COLLATE ', collation_name, ' ')
                    ELSE ' '
                  END
                , CASE 
                    WHEN is_nullable = 'NO' AND column_default IS NULL 
                        THEN 'NOT NULL '
                    WHEN is_nullable = 'NO' AND column_default IS NOT NULL 
                        THEN CONCAT('DEFAULT \'', column_default, '\' NOT NULL ')
                    WHEN is_nullable = 'YES' THEN 'DEFAULT NULL '
                  END
                ) AS cmd
                , column_name AS columnname
            FROM INFORMATION_SCHEMA.COLUMNS 
            WHERE table_schema = 'test'
            AND table_name = 'alphabet'
            ORDER BY columnname
        ) t1
        INNER JOIN (
            SELECT @rankmain:=@rankmain+1 AS rownum, position FROM (
                SELECT 0 AS rownum, 'FIRST' AS position
                    , '' AS columnname
                UNION
                SELECT @rank:=@rank+1 AS rownum, CONCAT('AFTER ', column_name) AS position
                    , column_name AS columnname
                FROM INFORMATION_SCHEMA.COLUMNS
                    WHERE table_schema = 'test'
                    AND table_name = 'alphabet'
                ORDER BY columnname
                LIMIT limitoffset
            ) inner_table
        ) t2 ON t1.rankalter = t2.rownum
    
        ;
    
    END//
    DELIMITER ;
    

    If anyone thinks/sees that I’m missing to include any important column attributes in the ALTER command, please hesitate not and mention it! Now to the next procedure. This one just executes the commands following the order of column id from the loadcommands table. :

    -- PROCEDURE TO RUN EACH REORDERING COMMAND
    DELIMITER //
    CREATE PROCEDURE reorder_executecommands ()
    BEGIN
        DECLARE sqlcommand VARCHAR(1000);
        DECLARE isdone INT DEFAULT FALSE;
    
        DECLARE reorderCursor CURSOR FOR
        SELECT sqlcmd FROM loadcommands ORDER BY id;
    
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET isdone = TRUE;
    
        OPEN reorderCursor;
        read_loop:LOOP
            FETCH reorderCursor INTO sqlcommand;
    
            IF isdone THEN
                LEAVE read_loop;
            END IF;
    
            SET @sqlcmd = sqlcommand;
            PREPARE stmt FROM @sqlcmd;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;          
    
        END LOOP read_loop;
    
        CLOSE reorderCursor;    
    END//
    DELIMITER ;
    

    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 alphabet table. Checking the results can be done using the SHOW... command.

    The last part:

    -- TO TEST; AFTER RUNNING DDL COMMANDS:
    
    SHOW CREATE TABLE alphabet;     -- SEE ORIGINAL ORDER
    CALL reorder_loadcommands();    -- PREPARE COMMANDS
    CALL reorder_executecommands(); -- RUN COMMANDS
    SHOW CREATE TABLE alphabet;     -- SEE NEW ORDER
    

    Perhaps later on I could make reorder_loadcommands dynamic and accept table and schema parameters, but I guess this is all for now..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an already built application and I want to add a feature that
I already have an application built in asp.net 3.5 and now I want to
So I have an input form that I want to use to update a
I could have sworn that there was an extension method already built for the
I have already built a site from scratch. It has banning, PM, comments, etc.
The basics have already been answered here . But is there a pre-built PHP
I have already googled for this I have a Table with following structure in
I have two tables that were built for two disparate systems. I have records
I have a Posts table in my Rails 3.0.10 app. I want to give
I have a situation where we want to prevent the update of a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.