2011-04-18 33 views
0

我有一個CSV文件看起來像幫助書面查詢

移動,消息
移動,消息
移動,消息

我有有

數據庫表

ID,手機
ID,手機
ID,移動

我正在讀的CSV成使用fgetcsv陣列。

我想直到結束是一個數組,看起來像

移動,消息,ID
移動,消息,ID
移動,消息,ID

一個查詢我可以做的是,在陣列中

的foreach行做select id from table where mobile = ...;

但看到有2000行,可以讓雜亂,

這樣做的更好方法是什麼?

回答

1

如果你想這樣做在PHP中,而不是在數據庫中,通過@Ignacio的建議,這裏有一個可能性:

/* ... $array = fgetcsv ... */ 

$mobiles = array_unique(array_map(create_function('$a', 'return $a["mobile"];'), $array)); 
$mobiles = array_map('mysql_real_escape_string', $mobiles); 

$query = "SELECT `id`, `mobile` FROM `table` 
      WHERE `mobile` IN ('" . join("', '", $mobiles) . "')"; 

/* ... $mobiles = mysql_query ... */ 

foreach ($array as &$row) { 
    foreach ($mobiles as $mobile) { 
     if ($row['mobile'] == $mobile['mobile']) { 
      $row['id'] = $mobile['id']; 
      continue 2; 
     } 
    } 
} 
0

將CSV文件加載到數據庫中,然後在兩個表上進行連接。