将 csv 文件导入到 MySQL 中具有外键和主键的 3 个表

Import csv file to 3 tables with foreign and primary keys in MySQL

我需要一些帮助来将 Excel table (.csv) 导入带有外键的 MySQL 数据库。

MySQLtable是:

测试 table

  test_ID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
  patient_ID INT DEFAULT NULL FOREIGN KEY (patient.id)
  test_type_ID INT DEFAULT NULL FOREIGN KEY (tests_types.id)
  result DOUBLE DEFAULT NULL
  comment VARCHAR(255) DEFAULT NULL

tests_types table

  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
  rf_nmu_ID INT DEFAULT NULL,
  loinc_od INT DEFAULT NULL,
  name VARCHAR(100) DEFAULT NULL,
  test_sourcename VARCHAR(255) DEFAULT NULL,

患者 table

  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY

来源 .csv 包含:

来源.csv:

+--------+--------------------+

| p_id | t1 | t2 | t3 | t4 |

+--------+-----+-----+-----+----+

| 1 | 123 | 4,4 | 0,83 | 37 |

+--------+-----+-----+-----+----+

| 2 | 124 | 4,3 | 0,86 | 36 |

+--------+-----+-----+-----+----+

| 3 | 146 | 4,4 | 0,99 | 44 |

+--------+-----+-----+-----+----+

| 4 | 96 | 3,5 | 0,82 | 30 |

+--------+-----+-----+-----+----+

| 5 | 116 | 4 | 0,87 | 38 |

+--------------------------------+

发布这个问题后,我了解到我可以 unpivot columns in Excel Power Questions making table 1D from 2D as you can see at这个 link:

将源 .csv table 从 2D 转换为 1D 后,我得到:

+--------+--------+--------+

| pat_id | test_id |值 |

+--------+--------+--------+

| 1 | 1 | 123 |

+--------+--------+--------+

| 1 | 2 | 4,4 |

+--------+--------+--------+

| 1 | 3 | 0,83 |

+--------+--------+--------+

| 1 | 4 | 37 |

+--------+--------+--------+

| 1 | 5 | 7 |

+--------+--------+--------+

和这个 table 我已经导入到我的 tests table 现在一切正常!