需要将价格表插入mysql

时间:2018-05-08 18:11:14

标签: mysql sql-insert bulkinsert

我有一张表,我需要在mysql数据库中添加一行新记录。现在我为每条记录手动执行此操作,但每个表中有600条记录。

我的行中的列必须是名为“curtain_entry_item”的数据库:

组>这是连接价格表的记录,例如这是价格表31。

宽度>这可能是20厘米30厘米等直到240

长度>这可能是20厘米30厘米等直到300

价格>这可能是30

那么我怎么能在一个动作中执行这个表到mysql数据库。

您可以在this link

查看该表的示例

非常感谢!沃特

2 个答案:

答案 0 :(得分:0)

由于没有确切的数据库结构细节,因此无法为您提供工作解决方案。

我的猜测是你需要INSERT INTO SELECT语句,它从一个表复制数据并将其插入另一个表中。您可以在以下网址找到w3scools的更多详细信息:

https://www.w3schools.com/sql/sql_insert_into_select.asp

答案 1 :(得分:0)

听起来你正在寻找的是一种将电子表格矩阵转换为一组加载数据库表的SQL命令的方法。

如果是这样,这更像是一个输入解析问题,而不是数据库问题。

一般方法是:

for each row
    for each column
        issue an INSERT statement using the row, column and intersection cell

这是一个Perl程序,可以使用您提供的电子表格的第一个标签(将其导出为CSV后)执行此操作:

#!/usr/bin/perl

# read in the header line and trim off whitespce
$header = <STDIN>;
$header =~ s/^\s+|\s+$//g;

# make the header line into an array of 'width' values
@widths = split(/,/, $header);

# loop through the remaining (data) rows
while ($data = <STDIN>) {
    # trim whitespace
    $data =~ s/^\s+|\s+$//g;

    # split the row into data elements, and use the 1st one as the 'length'
    @prices = split(/,/, $data);
    $length = $prices[0];

    # loop through the remaining data cells
    for ($i = 1; $i < 23; $i++) {
        # print out an appropriate INSERT statement
        printf(STDOUT "INSERT INTO curtain_price_table (group, width, length, price) VALUES (31, %d, %d, %d);\n", $widths[$i], $length, $prices[$i] . "\n");
    }
}

Perl不是你唯一的选择;任何脚本语言都可以很容易地完成同样的任务。而且,您可能需要对其进行调整,使其成为各种输入文件和数据库表的通用解决方案。但这应该为您提供足够的信息来解决您的需求。