sql >> Databáze >  >> RDS >> Mysql

PHP MYSQL importuje CSV a poté porovná a odstraní nadbytečné položky

Stačí mít účet se svými klíči.

Uložit každý $key v poli ve vašem while a nakonec spusťte dotaz, který říká

DELETE FROM tabel WHERE key NOT IN (listofcommaseparatedkeysgoeshere)
$arrayThatYouNeedToTest = array();
$handle = fopen($file,"r");
fgetcsv($handle, 1000, ",");//skip first row since they are headers
while(($fileop = fgetcsv($handle, 1000, ",")) !== false) //read line by line into $fileop
{
  //read array values into vars
  $item1 = $fileop[0];
  $item2 = $fileop[1];
  $key = $fileop[2];
  // and a couple more

  // now INSERT / UPDATE data in MySQL table
  $sql = mysql_query("INSERT INTO table (item1,item2,key) 
    VALUES ('$item1','$item2','$key') 
    ON DUPLICATE KEY UPDATE item1='$item1',item2='$item2'");

   $arrayThatYouNeedToTest[] = $key;    

}

$stringThatYouNeedToInspect = implode(",",$arrayThatYouNeedToTest);
$queryYouREALLYneedToCheckFirst = "DELETE FROM tabel WHERE key NOT IN  (".$stringThatYouNeedToInspect.")";

//$result = mysql_query($queryYouREALLYneedToCheckFirst);


  1. Funkce mysql_escape_string() je zastaralá, použijte mysql_real_escape_string() Codeigniter

  2. Jak DATE_SUB() funguje v MariaDB

  3. Case Expression vs Case Statement

  4. KNIME