sql >> Databáze >  >> RDS >> Mysql

Proměňte jeden dlouhý sloupec tabulky na tabulku s více sloupci

Řekněme, že tabulka se nazývá mytable a vypadá takto:

CREATE TABLE mytable
(
    X VARCHAR(10)
);

Pojďme do něj načíst ukázková data

mysql> drop database if exists user1267617;
Query OK, 3 rows affected (0.10 sec)

mysql> create database user1267617;
Query OK, 1 row affected (0.01 sec)

mysql> use user1267617
Database changed
mysql> CREATE TABLE mytable (X VARCHAR(10)
    -> );
Query OK, 0 rows affected (0.11 sec)

mysql>
mysql> insert into mytable values
    -> ('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),
    -> ('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),
    -> ('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z');
Query OK, 26 rows affected (0.06 sec)
Records: 26  Duplicates: 0  Warnings: 0

mysql> select * from mytable;
+------+
| X    |
+------+
| A    |
| B    |
| C    |
| D    |
| E    |
| F    |
| G    |
| H    |
| I    |
| J    |
| K    |
| L    |
| M    |
| N    |
| O    |
| P    |
| Q    |
| R    |
| S    |
| T    |
| U    |
| V    |
| W    |
| X    |
| Y    |
| Z    |
+------+
26 rows in set (0.00 sec)

mysql>

Zde je kód, který potřebujete:

CREATE TABLE mytmp
(
    id int not null auto_increment,
    X varchar(10),
    groupnum int,
    groupndx int,
    primary key (id)
);
INSERT INTO mytmp (X) select X from mytable;
UPDATE mytmp SET groupndx = MOD(id - 1,4),groupnum = FLOOR((id - 1)/4);
alter table mytmp add index (groupnum);
select * from mytmp;
CREATE TABLE mynewtable
(
    id int not null auto_increment,
    groupnum int,
    colA varchar(10) default '',
    colB varchar(10) default '',
    colC varchar(10) default '',
    colD varchar(10) default '',
    key (groupnum),
    primary key (id)
);
insert into mynewtable (colA,groupnum)
select X,groupnum from mytmp where groupndx = 0;
update mynewtable A INNER JOIN mytmp B ON A.groupnum=B.groupnum
AND B.groupndx=1 set A.colB = B.x;
update mynewtable A INNER JOIN mytmp B ON A.groupnum=B.groupnum
AND B.groupndx=2 set A.colC = B.x;
update mynewtable A INNER JOIN mytmp B ON A.groupnum=B.groupnum
AND B.groupndx=3 set A.colD = B.x;
alter table mynewtable drop column groupnum;
select * from mynewtable;

Zde je výsledek:

mysql> CREATE TABLE mytmp
    -> (
    ->     id int not null auto_increment,
    ->     X varchar(10),
    ->     groupnum int,
    ->     groupndx int,
    ->     primary key (id)
    -> );
Query OK, 0 rows affected (0.14 sec)

mysql> INSERT INTO mytmp (X) select X from mytable;
Query OK, 26 rows affected (0.07 sec)
Records: 26  Duplicates: 0  Warnings: 0

mysql> UPDATE mytmp SET groupndx = MOD(id - 1,4),groupnum = FLOOR((id - 1)/4);
Query OK, 26 rows affected (0.06 sec)
Rows matched: 26  Changed: 26  Warnings: 0

mysql> alter table mytmp add index (groupnum);
Query OK, 0 rows affected (0.23 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from mytmp;
+----+------+----------+----------+
| id | X    | groupnum | groupndx |
+----+------+----------+----------+
|  1 | A    |        0 |        0 |
|  2 | B    |        0 |        1 |
|  3 | C    |        0 |        2 |
|  4 | D    |        0 |        3 |
|  5 | E    |        1 |        0 |
|  6 | F    |        1 |        1 |
|  7 | G    |        1 |        2 |
|  8 | H    |        1 |        3 |
|  9 | I    |        2 |        0 |
| 10 | J    |        2 |        1 |
| 11 | K    |        2 |        2 |
| 12 | L    |        2 |        3 |
| 13 | M    |        3 |        0 |
| 14 | N    |        3 |        1 |
| 15 | O    |        3 |        2 |
| 16 | P    |        3 |        3 |
| 17 | Q    |        4 |        0 |
| 18 | R    |        4 |        1 |
| 19 | S    |        4 |        2 |
| 20 | T    |        4 |        3 |
| 21 | U    |        5 |        0 |
| 22 | V    |        5 |        1 |
| 23 | W    |        5 |        2 |
| 24 | X    |        5 |        3 |
| 25 | Y    |        6 |        0 |
| 26 | Z    |        6 |        1 |
+----+------+----------+----------+
26 rows in set (0.00 sec)

mysql> CREATE TABLE mynewtable
    -> (
    ->     id int not null auto_increment,
    ->     groupnum int,
    ->     colA varchar(10) default '',
    ->     colB varchar(10) default '',
    ->     colC varchar(10) default '',
    ->     colD varchar(10) default '',
    ->     key (groupnum),
    ->     primary key (id)
    -> );
Query OK, 0 rows affected (0.11 sec)

mysql> insert into mynewtable (colA,groupnum)
    -> select X,groupnum from mytmp where groupndx = 0;
Query OK, 7 rows affected (0.07 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> update mynewtable A INNER JOIN mytmp B ON A.groupnum=B.groupnum
    -> AND B.groupndx=1 set A.colB = B.x;
AND B.groupndx=3 set A.colD = B.x;
Query OK, 7 rows affected (0.07 sec)
Rows matched: 7  Changed: 7  Warnings: 0

mysql> update mynewtable A INNER JOIN mytmp B ON A.groupnum=B.groupnum
    -> AND B.groupndx=2 set A.colC = B.x;
Query OK, 6 rows affected (0.06 sec)
Rows matched: 6  Changed: 6  Warnings: 0

mysql> update mynewtable A INNER JOIN mytmp B ON A.groupnum=B.groupnum
    -> AND B.groupndx=3 set A.colD = B.x;
Query OK, 6 rows affected (0.06 sec)
Rows matched: 6  Changed: 6  Warnings: 0

mysql> alter table mynewtable drop column groupnum;
Query OK, 7 rows affected (0.26 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> select * from mynewtable;
+----+------+------+------+------+
| id | colA | colB | colC | colD |
+----+------+------+------+------+
|  1 | A    | B    | C    | D    |
|  2 | E    | F    | G    | H    |
|  3 | I    | J    | K    | L    |
|  4 | M    | N    | O    | P    |
|  5 | Q    | R    | S    | T    |
|  6 | U    | V    | W    | X    |
|  7 | Y    | Z    |      |      |
+----+------+------+------+------+
7 rows in set (0.00 sec)

mysql>

Vyzkoušejte to !!!




  1. Různé výsledky pomocí stejného dotazu s DB::raw a Eloquent

  2. Oracle na PostgreSQL — Kurzory a běžné tabulkové výrazy

  3. Jak volat uloženou proceduru z režimu spánku s parametry IN i OUT

  4. databáze přejmenování postgres nefunguje