0

我有一个名为的表system_settings,其中有 3 列,IDActual_Setting& Standards_Setting

在该ID列中有 3 行,'Virtual Machine 1''Virtual Machine 2'& 'Virtual Machine 3'

'Actual_Setting'我希望将文本文件中的数据导入ID = 'Virtual Machine 1'.

我试过使用这个查询:

LOAD DATA LOCAL IN FILE 'C:/Program Files/MySQL/MySQL Server 5.5/bin/syspolicy.txt'
INTO system_settings(Actual_Setting)
WHERE ID LIKE 'Virtual Machine 1';

但它返回一个错误。

我使用 MySQL Server 5.5、MySQL Query Browser 和一台 Windows 7 PC。

4

1 回答 1

0

LOAD DATA将始终将数据插入行,而不是更新现有行。

但是,如果发生键冲突,它可以通过使用其关键字首先删除现有行。REPLACE如果列上有UNIQUEID并且您不需要保留 的值Standards_Setting,则可以利用此功能:

LOAD DATA LOCAL INFILE 'C:/Program Files/MySQL/MySQL Server 5.5/bin/syspolicy.txt'
  REPLACE INTO TABLE system_settings
  FIELDS TERMINATED BY '' ENCLOSED BY '' ESCAPED BY ''
  LINES STARTING BY '' TERMINATED BY ''
  (Actual_Setting)
  SET ID = 'Virtual Machine 1'

(请注意,由于要将整个文件读入单个字段,因此必须覆盖默认值并明确指定没有字段或行终止/引用/转义字符。)

否则,如果您需要更新现有记录,我建议您将文件加载到临时表中,然后使用内容更新表:

CREATE TEMPORARY TABLE foo (content LONGTEXT);

LOAD DATA LOCAL INFILE 'C:/Program Files/MySQL/MySQL Server 5.5/bin/syspolicy.txt'
  INTO TABLE foo
  FIELDS TERMINATED BY '' ENCLOSED BY '' ESCAPED BY ''
  LINES STARTING BY '' TERMINATED BY ''
  (content);

UPDATE system_settings
  SET Actual_Setting = (SELECT content FROM foo)
WHERE ID = 'Virtual Machine 1';

DROP TEMPORARY TABLE foo;
于 2012-08-29T05:02:34.507 回答