我正在尝试使用LOAD DATA INFILE方法(csv数据到数据库)保存其中一列的递增值。我没有尝试任何工作。没有额外的serial_no字段,所有导入都很好,如果我将一个硬编码值设置为serial_no,那也可以。只是不能让它为每条记录增加1的数值。
-
$loadsqlfiletodb = "SET @a:=".$serial_no_max_new.";
LOAD DATA LOCAL INFILE '".$target_file."'
INTO TABLE ".DB_NAME2.".list_data
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(first,last,address,city,state,zip,company,tracking_number,vin,year,make,model,rough,average,clean,misc1,misc2,misc3,misc4,misc5,misc6,misc7,misc8,misc9,misc10,list,email,phone,image_name,image_path)
SET cid = ".$campaignid.",
jid = ".$jobid.",
serial_no = @a:=@a+1 ";
mysql_query($loadsqlfiletodb) or die(mysql_error());
-
$loadsqlfiletodb = "
SET @a:='".$serial_no_max_new."'
LOAD DATA LOCAL INFILE '".$target_file."'
INTO TABLE ".DB_NAME2.".list_data
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(first,last,address,city,state,zip,company,tracking_number,vin,year,make,model,rough,average,clean,misc1,misc2,misc3,misc4,misc5,misc6,misc7,misc8,misc9,misc10,list,email,phone,image_name,image_path)
SET cid = ".$campaignid.",
jid = ".$jobid.",
serial_no = @a:=@a+1 ";
mysql_query($loadsqlfiletodb) or die(mysql_error());
-
$loadsqlfiletodb = "LOAD DATA LOCAL INFILE '".$target_file."'
INTO TABLE ".DB_NAME2.".list_data
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(first,last,address,city,state,zip,company,tracking_number,vin,year,make,model,rough,average,clean,misc1,misc2,misc3,misc4,misc5,misc6,misc7,misc8,misc9,misc10,list,email,phone,image_name,image_path)
SET cid = ".$campaignid.",
jid = ".$jobid.",
serial_no = ".$serial_no_max_new."+1 ";
mysql_query($loadsqlfiletodb) or die(mysql_error());
-
$loadsqlfiletodb = "LOAD DATA LOCAL INFILE '".$target_file."'
INTO TABLE ".DB_NAME2.".list_data
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(first,last,address,city,state,zip,company,tracking_number,vin,year,make,model,rough,average,clean,misc1,misc2,misc3,misc4,misc5,misc6,misc7,misc8,misc9,misc10,list,email,phone,image_name,image_path)
SET cid = ".$campaignid.",
jid = ".$jobid.",
serial_no = ".$serial_no_max_new." = ".$serial_no_max_new."+1 ";
mysql_query($loadsqlfiletodb) or die(mysql_error());
更新
这是完整的事情。序列号始终为9位数。不多也不少。当串口达到999999999时,下一个记录的序列将是000000000.但是,不要担心9到9的零点。此时我正在尝试使用将文件导入数据库的LOAD DATA INFILE方法计算出该列的值增加1。
$query_max = mysql_fetch_array(mysql_query("SELECT id, latest_serial_no FROM ".DB_NAME2.".latest_serial_number WHERE id=1"), MYSQL_ASSOC);
$serial_no_max = $query_max["latest_serial_no"];
$serial_no_max_new = $serial_no_max;
if ($serial_no_max > 999999998) {$serial_no_max_new = 000000000;}
$padd = "";
$serial_len = strlen($serial_no_max_new);
if($serial_len < 9) {
$serial_len = (9 - $serial_len);
for($is=0;$is<$serial_len;$is++) {
$padd .= "0";
}
$serial_no_max_new = $padd . $serial_no_max_new;
}
$query_serial = trim($serial_no_max_new);
//echo "q_s: ".$query_serial;
//Import uploaded file to Database
//$target_file is where the uploaded file is located
$loadsqlfiletodb = "SET @a:=".$query_serial.";
LOAD DATA LOCAL INFILE '".$target_file."'
INTO TABLE ".DB_NAME2.".list_data
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(first,last,address,city,state,zip,company,tracking_number,vin,year,make,model,rough,average,clean,misc1,misc2,misc3,misc4,misc5,misc6,misc7,misc8,misc9,misc10,list,email,phone,image_name,image_path)
SET cid = ".$campaignid.",
jid = ".$jobid.",
serial_no = @a:=@a+1 ";
mysql_query($loadsqlfiletodb) or die(mysql_error());
答案 0 :(得分:1)
也许是这样的:
/*1 get last serial number.
open csv file.
foreach row in csv file:
increment serial number.
if max number
set serial number to 0.
add new field to csv row
close csv file
insert csv to database*/
$query_max = mysql_fetch_array(mysql_query("SELECT id, latest_serial_no FROM ".DB_NAME2.".latest_serial_number WHERE id=1"), MYSQL_ASSOC);
$serial_no_max = $query_max["latest_serial_no"];
$serial_no_max_new = $serial_no_max;
if ($serial_no_max > 999999998) {$serial_no_max_new = 000000000;}
$padd = "";
$serial_len = strlen($serial_no_max_new);
if($serial_len < 9) {
$serial_len = (9 - $serial_len);
for($is=0;$is<$serial_len;$is++) {
$padd .= "0";
}
$serial_no_max_new = $padd . $serial_no_max_new;
}
$query_serial = trim($serial_no_max_new);
//echo "q_s: ".$query_serial;
// Add serail number to csv code
$csv = fopen($target_file, "r");
$tmp_csv_addr = $target_file . "2";
$tmp_csv = fopen($tmp_csv_addr, "a");
while (($data = fgetcsv($csv, 0, ",")) !== FALSE) {
$data[] = $query_serial;
fputcsv($tmp_csv, $data, ",");
}
fclose($csv);
fclose($tmp_csv);
// Insert into database
$loadsqlfiletodb = "LOAD DATA LOCAL INFILE '".$tmp_csv_addr."'
INTO TABLE ".DB_NAME2.".list_data
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(first,last,address,city,state,zip,company,tracking_number,vin,year,make,model,rough,average,clean,misc1,misc2,misc3,misc4,misc5,misc6,misc7,misc8,misc9,misc10,list,email,phone,image_name,image_path,serial_no)
SET cid = ".$campaignid.",
jid = ".$jobid;
mysql_query($loadsqlfiletodb) or die(mysql_error());
unlink($tmp_csv);
答案 1 :(得分:0)
使用auto_incement列(id)将数据加载到表中。
之后运行更新:
UPDATE list_data
SET serail_no = MOD(id, 1000000000)