模式匹配我错在哪里?

时间:2017-07-22 18:19:36

标签: php mysql sql

我将此数据插入名为princess_bet_football的表中 TABLE ONE TABLE NAME:princess_bet_football ID_NO TIME TEAMS HOME DRAW AWAY 131 23:30:00 UTC DE CAJAMARCA v Sport Rosario 1.57 3.45 6.28 132 23:30:00 Atletico FC v Cucuta Deportivo 4.13 3.14 1.88 133 01:15:00 America Mineiro MG v Figueirense FC 1.58 3.38 6.33 134 02:00:00 EC Taubate SP v EC Agua Santa SP 2.44 3.09 2.81 135 02:00:00 New York Red Bulls II v Toronto FC II 1.56 3.73 5.44 136 02:30:00 Santiago Wanderers v Cobresal 1.56 3.78 5.44 137 02:30:00 Luverdense MT v Parana Clube PR 2.11 2.95 3.64 138 02:30:00 Santa Cruz PE v Boa Esporte Clube 1.90 3.06 4.27 139 03:00:00 Monarcas Morelia v CF Monterrey 3.17 3.06 2.22 140 03:00:00 Botafogo PB v Fortaleza CE 2.06 3.06 3.59

有第二个名为mk_bet的表是空的但是有一些原始数据要插入到csv中,这些数据如下所示

TABLE TWO Atletico FC v Cucuta Deportivo 4 3.5 1.75 Deportivo Capiata v Nacional Montevideo 2.3 3.2 2.8 Americade Quito v Aucas 2.38 3.2 2.7 America Mineiro v Figueirense 1.62 3.5 4.75 EC Taubate v Agua Santa 2.3 3.3 2.63 Orlando City v Atlanta United 2.6 3.25 2.4 General Diaz v Cerro Porteno 3.8 3.25 1.83 Santa Cruz v Boa 1.91 3.3 3.6 Santiago Wanderers v Cobresal 1.5 4.2 5 Monarcas Morelia v Monterrey 3.25 3.2 2.05 Ceara v Goias 1.73 3.3 4.33 LDU Loja v Olmedo 1.36 4.33 7 Barcelona SC v Fuerza Amarilla 1.2 5.25 13 Tigre v Patriotas 2.5 2.8 2.8 Atlante v Cafetalerosde Tapachula 1.95 3.25 3.5 Boyaca Chico v Universitario Popayan 1.67 3.5 4.5 Leones Negros v Correcaminos 1.95 3.3 3.4 Venados v Minerosde Zacatecas 2.2 3.3 2.88 Tijuana v Cruz Azul 2.15 3.1 3.1 Avondale Heights U20 v Bentleigh Greens U20 1.75 4 3.5

因此,为了插入要插入表2的数据,它必须存在于表1中,有一个执行该任务的查询,如下所示

 PHP CODES

$csv_file = CSV_PATH . "mkbf.csv"; 
$row = 0;

if (($handle = fopen($csv_file, "r")) !== FALSE) {
 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {   
    $row++;
    if($row == 1) continue;
    if ($data[0]) { 
     //Mysql Start
     $query = mysql_query("SELECT * FROM `princess_bet_football` WHERE `TEAMS` LIKE '%$data[0]%'");
    if(mysql_num_rows($query) > 0){
     while ($newArray = mysql_fetch_array($query))
     {
       $id_no=$newArray['ID_NO'];
       $sql="insert into mkeka_bet_football (ID_NO,TEAMS,HOME,DRAW,AWAY)VALUES('$id_no','".addslashes($data[0])."','".addslashes($data[1])."','".addslashes($data[2])."','".addslashes($data[3])."')"; 

     if (mysql_query($sql))
     {
     echo "<b>";
     echo "RECORD(S) ADDED ";
     echo "</b>";
     }
     else
     {
       echo "<b>";
       echo "RECORD(S) NOT ENTERED SOMETHING WENT WRONG";
       echo "</b>";
       echo "<br>";
       echo mysql_error();
     }
  }
}
//Mysql End
} 
}
fclose($handle);

如上所示,表1中已经包含数据表两个为空,为了在表2中插入数据,表中应该存在一个记录,在php代码上面它做的工作是检查记录是否插入表二存在于表一,如果它存在,它将被插入表二,如果它不存在它将不会被插入,所以我的问题来自上面的PHP代码,它确实检查记录是否存在但它的一致性,例如,如果我采取这些代码并且在我的phpadmin中运行只有两个记录将输入到表2中,而它应该输入表中存在的五个记录,表二希望我的解释现在清楚,任何想法或新想法如何实现这个???

1 个答案:

答案 0 :(得分:0)

将文件转储到临时表中并使用此查询 INSERT INTO mkeka_bet_football(ID_NO,TEAMS,HOME,DRAW,AWAY)SELECT A.ID_NO,A.TEAMS,A.HOME,A.DRAW,A.AWAY from princess_bet_football A JOIN mkeka_bet_football_temp B ON A.TEAMS = B.TEAMS; < / p>