MySQL按列名排序表

时间:2011-11-21 10:10:34

标签: mysql

我已经使用任意顺序构建了一个包含字段名称的表。我希望这些字段名称按字母顺序排列,以便我可以在我的下拉列表中使用它们。是否可以使用查询?

3 个答案:

答案 0 :(得分:6)

使用INFORMATION_SCHEMA.COLUMNS从特定表格中选择列,并使用ORDER BY按字母顺序排序:

    SELECT column_name
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE table_schema = '[schemaname]'
    AND table_name = '[tablename]'
    ORDER BY column_name

答案 1 :(得分:1)

注意:以下代码将更改指定的表并按字母顺序对列重新排序


这应该可以解决问题。它有点混乱和冗长,你将不得不更改数据库名称和表名,但对于这个,唯一的要求是有一个名为“test”的数据库,并且你正在运行这些命令:< / p>

让我们创建我们需要的表格:

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

现在让我们创建这个工作所需的两个存储过程:

将它们分开,因为一个人将负责加载命令,并包括一个光标以立即使用它是不合理的(至少对我和我的mysql版本):

-- 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 ;

如果有人认为/我发现我缺少在ALTER命令中包含任何重要的列属性,请不要犹豫,并提及它!现在进入下一个程序。这个只是按照id表中列loadcommands的顺序执行命令。 :

-- 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 ;

SQL很长,所以如果有人可以指出方法(并且已经测试过它们)来缩短它,我很乐意这样做,但是现在,这至少可以在我的方面起作用。我也不需要在alphabet表中放置虚拟数据。可以使用SHOW...命令检查结果。

最后一部分:

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

也许稍后我可以使reorder_loadcommands动态并接受表格和架构参数,但我想这一切都是现在......

答案 2 :(得分:-1)

这必须帮助

ALTER table `table_name`
   MODIFY COLUMN `Field1` varchar(100)
   AFTER `Field2`

您将在 Field2

之后获得 Field1

Field2
FIELD1