总计的文件的SUM行和使用total的make查询。 PHP MSSQL

时间:2014-12-10 11:59:16

标签: php sql-server arrays file

我有一个像这样的文件

FG  09097612 DN 6575 HL    879797
BHC 09097613 DN 6576 HL    879798
FG  09097614 DN 6577 IOPPP 879799
FG  09097614 DN 6577 IOPPP 879800

我用

在mysql中导入它
$lines = file("upload/import.txt");
$dbhandle = odbc_connect("Driver={SQL Server Native Client 11.0};
Server=$myServer;Database=$myDB;", $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
$query = "INSERT INTO TEST (nation) VALUES \n";
$row = array(); // query for each line
foreach($lines as $lineNum => $line ) {
    $nation = trim(substr($line, 0, 4)); // get first four characters as nation and remove spaces
    $prize = trim(substr($line, 4, 8)); // get 5th-12th characters as prize and remove spaces
    $player = trim(substr($line, 12, 2)); // get 13th-14th characters as player and remove spaces
    $row []= "(".$nation.")";
}
$query .= implode(",\n",$row).";";
$result = odbc_exec($dbhandle, $query);

我现在需要检查文件中是否有相同国家的行,在此示例中为“FG”并将每个“FG”的$prize相加并仅保存奖金中的总数而不是FG的所有线路?

3 个答案:

答案 0 :(得分:1)

如果您需要在php中执行此操作,我会将行保存在由国家/地区键入的数组中,并且每次检查该键是否已存在,如果是,则只增加奖励。

这样的事情: -

<?php

$lines = file("upload/import.txt");
$dbhandle = odbc_connect("Driver={SQL Server Native Client 11.0};
Server=$myServer;Database=$myDB;", $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
$query = "INSERT INTO TEST (nation, prize) VALUES \n";
$row = array(); // query for each line
foreach($lines as $lineNum => $line ) 
{
    $nation = trim(substr($line, 0, 4)); // get first four characters as nation and remove spaces
    if (array_key_exists($nation, $row))
    {
        $row[$nation]['prize'] += $prize;
    }
    else
    {
        $prize = trim(substr($line, 4, 8)); // get 5th-12th characters as prize and remove spaces
        $player = trim(substr($line, 12, 2)); // get 13th-14th characters as player and remove spaces
        $row[$nation]= array('nation'=>$nation, 'prize'=>$prize);
    }
}
array_walk($row, function($v, $k){return "(".$v['nation'].", ".$v['prize'].")";});
$query .= implode(",\n",$row).";";
$result = odbc_exec($dbhandle, $query);

?>

然而,如果这是在MySQL中完成的话,我很想将国家作为数据库中的唯一键,并将 ON DUPLICATE KEY SET prize = prize + VALUES(prize)添加到插入查询的结尾。

答案 1 :(得分:0)

您可以编写非常复杂的PHP代码来执行此操作。或者,您可以改变策略。首先,我要指出您可以使用load data infile来读取文件中的数据。

新策略是将数据读入临时表,然后将其复制到最终表。基本上是:

read data into test_staging table (using php or `load data infile`)

insert into test(nation, prize)
    select nation, sum(prize)
    from test_staging
    group by nation;

drop table test_staging;

答案 2 :(得分:0)

因此,如果您需要在PHP中使用它,我建议使用这样的命名数组:

$insertLines = array();
foreach($lines as $lineNum => $line ) {
    $nation = trim(substr($line, 0, 4)); // get first four characters as nation and remove spaces
    $prize = trim(substr($line, 4, 8)); // get 5th-12th characters as prize and remove spaces
    insertLines[$nation] += $prize;
}
foreach($insertLines as $lineNation => $linePrice ) {
    // The creation of the insert into the database goes here.
}

请检查长度。 我认为平板电脑是14到15?

$player = trim(substr($line, 13, 2));