duanque3125 2013-05-11 12:06
浏览 76

插入WHERE NOT EXISTS

I have a table that I regularly update by inserting a csv file which is dumped from another source. This file contains exisiting data in my table as well as new data. What I currently do is delete the contents of the table that relate to the data dump and re-import using the following php script:

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());
}
}

?>

What I really want to do is to just insert the data that does not currently exsit in the table. I have tried to do this via adding:

WHERE NOT EXISTS (SELECT * from stats_table)

but this is throwing a syntax error...

Could you please suggest a method of doing this.

  • 写回答

2条回答 默认 最新

  • douren1928 2013-05-11 12:10
    关注

    May be you want this:

    WHERE field NOT IN (SELECT field FROM stats_table) ;
    

    Also you may take a look at MySQL SELECT x FROM a WHERE NOT IN ( SELECT x FROM b ) - Unexpected result

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?