我有一个表,我通过插入从其他来源转储的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)
但这会引发语法错误......
请您建议一种方法。
答案 0 :(得分:1)
您可以使用
Insert ...On Duplicate key update
请参阅以下链接,它可能会对您有所帮助
或者你可以看到这篇文章
希望这会有所帮助! :)答案 1 :(得分:0)
可能你想要这个:
WHERE field NOT IN (SELECT field FROM stats_table) ;
你也可以看看 MySQL SELECT x FROM a WHERE NOT IN ( SELECT x FROM b ) - Unexpected result