1

我有一个表格,我通过插入一个从另一个来源转储的 csv 文件来定期更新它。该文件包含我的表中的现有数据以及新数据。我目前所做的是删除与数据转储相关的表内容,然后使用以下 php 脚本重新导入:

if(isset($_POST['submit']))
{
$file = $_FILES['file']['tmp_name'];

$handle = fopen($file,"r");

while(($fileop = fgetcsv($handle,1000, ",")) !== false)
{
    $player_id = $fileop[1];
    $fixture_id = $fileop[3];
    $opponent = $fileop[6];
    $home_away = $fileop[7];
    $team_id = $fileop[8];
    $season_id = $fileop[9];
    $fixture_date = $fileop[11];
    $runs = $fileop[15];
    $sixes = $fileop[17];
    $fours = $fileop[16];
    $how_out = $fileop[25];
    $wk_catches = $fileop[26];
    $fielder_catches = $fileop[27];
    $fielder_stumpings = $fileop[28];
    $_fielder_runouts = $fileop[29];
    $wickets = $fileop[30];
    $bowling_runs = $fileop[31];
    $balls_bowled = $fileop[32];
    $maidens = $fileop[33];
    $bowled = $fileop[37];
    $caught = $fileop[38];
    $lbw = $fileop[39];
    $stumped = $fileop[40];
    $position = $fileop[47];

    $sql= mysql_query("INSERT INTO stats_game (player_id, fixture_id, opponent, home_away, team_id, season_id, fixture_date, runs, fours, sixes, how_out, wk_catches, fielder_catches, fielder_stumpings, _fielder_runouts, wickets, bowling_runs, balls_bowled, maidens, bowled, caught, lbw, stumped, position) VALUES ('$player_id', '$fixture_id', '$opponent', '$home_away', '$team_id', '$season_id', '$fixture_date', '$runs', '$fours', '$sixes', '$how_out', '$wk_catches', '$fielder_catches', '$fielder_stumpings', '$_fielder_runouts', '$wickets', '$bowling_runs', '$balls_bowled', '$maidens', '$bowled', '$caught', '$lbw', '$stumped', '$position') WHERE NOT EXISTS (SELECT * from stats_game)");
}
if($sql){
    echo 'data uploaded successfully';}else{
        die(mysql_error());
}
}

?>

我真正想做的只是插入表中当前不存在的数据。我试图通过添加来做到这一点:

WHERE NOT EXISTS (SELECT * from stats_table)

但这会引发语法错误...

你能否建议一种方法来做到这一点。

4

2 回答 2

1

您可以使用

Insert ...On Duplicate key update 

请参考以下链接,它可能对您有所帮助

或者你可以看到这个帖子

希望这会有所帮助!:)

于 2013-05-11T12:15:28.057 回答
0

可能你想要这个:

WHERE field NOT IN (SELECT field FROM stats_table) ;

你也可以看看 MySQL SELECT x FROM a WHERE NOT IN (SELECT x FROM b) - Unexpected result

于 2013-05-11T12:10:55.283 回答