Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将XLS数据导入MySQL表,保持第一列为PK_Mysql_Excel_Import - Fatal编程技术网

将XLS数据导入MySQL表,保持第一列为PK

将XLS数据导入MySQL表,保持第一列为PK,mysql,excel,import,Mysql,Excel,Import,我得到了一个Excel电子表格,要导入MySQL数据库。 每个Excel行都有许多列,其中第一列是主键,其余的是应作为单独行输入数据库的值。例如: | SizeGroup | Size1 | Size2 | Size3 | Size4 | Size5 | ===================================================== | A | S | M | L | | | --------------

我得到了一个Excel电子表格,要导入MySQL数据库。 每个Excel行都有许多列,其中第一列是主键,其余的是应作为单独行输入数据库的值。例如:

| SizeGroup | Size1 | Size2 | Size3 | Size4 | Size5 |
=====================================================
|     A     |   S   |   M   |   L   |       |       |
-----------------------------------------------------
|     B     |   S   |   M   |   L   |  XL   |       |
-----------------------------------------------------
|     C     |   S   |   M   |  XXL  |       |       |
-----------------------------------------------------
|     D     |   36  |   37  |  38   |  39   |  40   |
-----------------------------------------------------
|     E     |   M   |   XL  |       |       |       |
-----------------------------------------------------
|     F     |   39  |   40  |   41  |   42  |  43   |
-----------------------------------------------------
预期的结果将是:

Table: sizes
id | size_group | size
=======================
 1 |      A     |  S
-----------------------
 2 |      A     |  M
-----------------------
 3 |      A     |  L
-----------------------
 4 |      B     |  S
-----------------------
 5 |      B     |  M
-----------------------
 6 |      B     |  L
-----------------------
 7 |      B     |  XL
-----------------------

如何做到这一点?我可以用PHP编写一些代码,使用库读取xls并写入db,但肯定有更好的解决方案。

您可以在Mysql中导入excel工作表,如下所示:

导入数据后,指定primarykey,如下所示:

ALTER TABLE tablename ADD PRIMARY KEY(colname);