MySQL - 在LOAD DATA INFILE上设置多个

时间:2012-09-21 12:51:41

标签: mysql csv load

我有table_name这样的话:

No | Name | Inserted_Date | Inserted_By
=====================================

然后我就像这样提交name.csv

no,name
1,jhon
2,alex
3,steve

我想使用以下语法加载这些文件table_name

LOAD DATA INFILE 'name.csv' INTO TABLE table1
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
???

问题是,我应该把???放在哪里,这样我就可以存储这样的数据:

No | Name  | Inserted_Date | Inserted_By
=====================================
1  | jhon  | sysdate()     | me
2  | ales  | sysdate()     | me
3  | steve | sysdate()     | me

5 个答案:

答案 0 :(得分:20)

我不明白您的表中是否已存在列inserted_dateinserted_by。如果不是,您可以在运行LOAD DATA INFILE之前添加它们:

LOAD DATA INFILE 'name.csv' INTO TABLE table1
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(@no, @name)
set
  no = @no,
  name = @name,
  inserted_date = now(),
  inserted_by = 'me'

答案 1 :(得分:2)

这样的事情会做到:

LOAD DATA INFILE 'name.csv' INTO TABLE table1 
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
    SET inserted_date=CURRENT_DATE(), inserted_by='me'

看看手册: http://dev.mysql.com/doc/refman/5.1/en/load-data.html

答案 2 :(得分:0)

在数据库中以1分钟为单位插入超过7000000条记录(计算超高速查询)

mysqli_query($cons, '
    LOAD DATA LOCAL INFILE "'.$file.'"
    INTO TABLE tablename
    FIELDS TERMINATED by \',\'
    LINES TERMINATED BY \'\n\'
    IGNORE 1 LINES
    (isbn10,isbn13,price,discount,free_stock,report,report_date)
     SET RRP = IF(discount = 0.00,price-price * 45/100,IF(discount = 0.01,price,IF(discount != 0.00,price-price * discount/100,@RRP))),
         RRP_nl = RRP * 1.44 + 8,
         RRP_bl = RRP * 1.44 + 8,
         ID = NULL
    ')or die(mysqli_error());
$affected = (int) (mysqli_affected_rows($cons))-1; 
$log->lwrite('Inventory.CSV to database:'. $affected.' record inserted successfully.');

RRP和RRP_nl以及RRP_bl不在csv中,但是在插入之后我们计算出来。

答案 3 :(得分:-1)

使用mysql中的load data命令无法做到这一点。数据必须在您的输入文件中。

如果您使用的是Linux,则可以使用sed或类似工具将其他字段数据添加到输入文件中。

另一种方法是上传您的文件,然后运行查询以使用您想要的数据更新缺少的字段。

您还可以在表上设置触发器,以便在插入发生时填充这些字段,假设您要使用mysql用户名值。

答案 4 :(得分:-1)

<html>
  <body>
    <div class="changepic-wrap">
      <form action="changepicauth.php" method="post">
        <input type="file" name="profilepic" id="profilepic" class="inputfile">
        <br>
        <label for="profilepic">
          <img src="#" />
          Upload Picture...
        </label>
        <br>
        <div class="button-wrap">
          <button>Change Picture</button>
        </div>
      </form>
    </div>

    <script>
      var profilePic = document.getElementById('profilepic'); /* finds the input */

      function changeLabelText() {
        var profilePicValue = profilePic.value; /* gets the filepath and filename from the input */
        var fileNameStart = profilePicValue.lastIndexOf('\\'); /* finds the end of the filepath */
        profilePicValue = profilePicValue.substr(fileNameStart + 1); /* isolates the filename */
        var profilePicLabelText = document.querySelector('label[for="profilepic"]').childNodes[2]; /* finds the label text */
        if (profilePicValue !== '') {
            profilePicLabelText.textContent = profilePicValue; /* changes the label text */
        }
      }


      profilePic.addEventListener('change',changeLabelText,false); /* runs the function whenever the filename in the input is changed */
    </script>
  </body>
</html>