从其他表传输数据并将字段名称设置为值

时间:2013-12-01 00:55:56

标签: mysql

我有查询问题

我有一张桌子

 -------------------------------------------------------------------------------
| coding | prov | kota | kecamatan | kode | reg | etd_reg | oke | etd_oke | yes | 
 -------------------------------------------------------------------------------
| 1      | a    | a_1   | a_1_1    |   A  | 20  | 1 -2    | 22  | 2 -3    | 25  |
| 2      | a    | a_1   | a_1_2    |   A  | 19  | 2 -3    | 21  | 3 -5    | 24  |
| 3      | a    | a_1   | a_1_3    |   A  | 21  | 3 -5    | 23  | 5 -7    | 26  |
| 4      | a    | a_2   | a_2_1    |   A  | 22  | 1 -2    | 24  | 2 -3    | 27  |
| 5      | a    | a_2   | a_2_2    |   A  | 20  | 2 -4    | 22  | 4 -6    | 25  |
| 6      | b    | b_1   | b_1_1    |   B  | 22  | 7 -9    | -   | -       | -   |
| 7      | b    | b_1   | b_1_2    |   B  | 15  | 4 -5    | 17  | 6 -7    | 20  |
| 8      | b    | b_2   | b_2_1    |   B  | 27  | 2 -4    | -   | -       | -   |
| 9      | b    | b_2   | b_2_2    |   B  | 11  | 2 -3    | 13  | 3 -5    | 16  |
| 10     | b    | b_2   | b_2_3    |   B  | 23  | 1 -2    | 25  | 2 -3    | -   |
 -------------------------------------------------------------------------------

我想将该表的数据传输到新表,如下所示

 ---------------------------------------------------------------------
| id | coding | prov | kota | kecamatan | kode | paket | cost |  etd  | 
 ---------------------------------------------------------------------
|  1 | 1      | a    | a_1  | a_1_1     | A    | reg   | 20   |  1 -2 | 
|  2 | 1      | a    | a_1  | a_1_1     | A    | oke   | 22   |  2 -3 | 
|  3 | 1      | a    | a_1  | a_1_1     | A    | yes   | 25   |  1    | 
|  4 | 2      | a    | a_1  | a_1_2     | A    | reg   | 19   |  2 -3 | 
|  5 | 2      | a    | a_1  | a_1_2     | A    | oke   | 21   |  3 -5 | 
|  6 | 2      | a    | a_1  | a_1_2     | A    | yes   | 24   |  1    | 
|  7 | 3      | a    | a_1  | a_1_3     | A    | reg   | 21   |  3 -5 | 
|  8 | 3      | a    | a_1  | a_1_3     | A    | oke   | 23   |  5 -7 | 
|  9 | 3      | a    | a_1  | a_1_3     | A    | yes   | 26   |  1    | 
| 10 | 4      | a    | a_2  | a_2_1     | A    | reg   | 22   |  1 -2 | 
| 11 | 4      | a    | a_2  | a_2_1     | A    | oke   | 24   |  2 -3 | 
| 12 | 4      | a    | a_2  | a_2_1     | A    | yes   | 27   |  1    | 
| 10 | 5      | a    | a_2  | a_2_2     | A    | reg   | 20   |  2 -4 | 
| 11 | 5      | a    | a_2  | a_2_2     | A    | oke   | 22   |  4 -6 | 
| 12 | 5      | a    | a_2  | a_2_2     | A    | yes   | 25   |  1    | 
| 13 | 6      | b    | b_1  | b_1_1     | B    | reg   | 22   |  7 -9 | 
| 14 | 7      | b    | b_1  | b_1_2     | B    | reg   | 15   |  4 -5 | 
| 15 | 7      | b    | b_1  | b_1_2     | B    | oke   | 17   |  6 -7 | 
| 16 | 7      | b    | b_1  | b_1_2     | B    | yes   | 20   |  1    | 
| 17 | 8      | b    | b_2  | b_2_1     | B    | reg   | 27   |  2 -4 | 
| 14 | 9      | b    | b_2  | b_2_2     | B    | reg   | 11   |  2 -3 | 
| 15 | 9      | b    | b_2  | b_2_2     | B    | oke   | 13   |  3 -5 | 
| 16 | 9      | b    | b_2  | b_2_2     | B    | yes   | 16   |  1    | 
| 17 | 10     | b    | b_2  | b_2_3     | B    | reg   | 23   |  1 -2 | 
| 19 | 10     | b    | b_2  | b_2_3     | B    | oke   | 25   |  2 -3 | 
 ---------------------------------------------------------------------

实际数据更多是5000行..是否可以转移它?

如果字段的值是 - 不需要创建新行

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

首先,您必须创建新表。 EG:

CREATE TABLE RESULT(
  id int NOT NULL AUTO_INCREMENT, coding int, prov varchar(1), kota varchar(3),
  kecamatan varchar(5), kode varchar(1), paket varchar(3), cost int,
  etd varchar(4), PRIMARY KEY (id));

请注意,此表格会将费用包含为int(比字符串好得多,对吗?)所以您以后必须将其转换(正如您在下面的查询中看到的那样)

然后,您必须根据kode的不同类型在此新表和联合中插入数据。 EG:

INSERT INTO RESULT (coding, prov, kota, kecamatan, kode, paket, cost, etd)
SELECT coding, prov, kota, kecamatan, kode, 'reg' paket, reg+0 cost, etd_reg etd
FROM t
UNION ALL
SELECT coding, prov, kota, kecamatan, kode, 'oke' paket, oke+0 cost, etd_oke etd
FROM t
WHERE oke != '-'
UNION ALL
SELECT coding, prov, kota, kecamatan, kode, 'yes' paket, yes+0 cost, 1 etd
FROM t
WHERE yes != '-'

您可以在此fiddle中看到这一点。