MySQL-如何连接加载数据语句?

时间:2018-11-20 07:47:57

标签: php mysql sql

我有这句话。在我需要公司字段为“ stackoverflow”而不是CSV值的地方。

上传的csv

alice,bob,support@stackoverflow.com,+3233232,companyx,appointment
alice1,bob1,support1@stackoverflow.com,+3233232,companyy,appointment

SQL:

$sql = <<<eof
  LOAD DATA INFILE '$target_path'
   INTO TABLE sh_contact
   FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
   LINES TERMINATED BY '\n'
  (firstname,lastname,email,mobile,company,type)
eof;

编辑:

LOAD DATA INFILE '$target_path'
INTO TABLE sh_contact
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(firstname, lastname, email, mobile, company, type)
SET company = 'stackoverflow', type='blabla';

1 个答案:

答案 0 :(得分:1)

company列中使用变量占位符,然后将公司手动覆盖为Stack Overflow:

LOAD DATA INFILE '$target_path'
INTO TABLE sh_contact
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(firstname, lastname, email, mobile, @var, type)
SET company = 'stackoverflow';

在这种情况下,您甚至可能不需要使用@var。无论如何,您不需要知道输入值是什么,因为您打算覆盖它。