2017-05-18 65 views
0

的數組如何在SQL查詢中運行:通過數組
選擇字段凡ID_del_hotelOcupacionHO> =比賽變數Array[ID_del_hotel, Ocupacion, HO]PHP SQL查詢哪裏陣列

$array= 

Array 
(
    [0] => Array 
     (
      [ID_del_hotel] => 45 
      [Ocupacion] => SGL 
      [HO] => 1 
     ) 

    [1] => Array 
     (
      [ID_del_hotel] => 44 
      [Ocupacion] => DBL 
      [HO] => 2 
     ) 

    [2] => Array 
     (
      [ID_del_hotel] => 44 
      [Ocupacion] => QDP 
      [HO] => 3 
     ) 

) 
+1

循環數組構建查詢 – rtfm

+1

你的問題不明確。顯示你的數據庫表模式。 –

回答

1

河套構建在IN子句的ID

MySQL的

WHERE db_id IN (45,46,47) 
+0

和其他變量? – rtfm

+0

你對數據庫速度有多關心?你可以對值做一個CONCAT,並且有點髒。 CONCAT(id,「 - 」,name,「 - 」等)IN(valuelist)/但是如果你這樣做,你將失去索引性能。 –

0

如果我瞭解您的查詢。

$arr = Array 
    (
     [0] => Array 
      (
       [ID_del_hotel] => 45 
       [Ocupacion] => SGL 
       [HO] => 1 
      ) 

     [1] => Array 
      (
       [ID_del_hotel] => 44 
       [Ocupacion] => DBL 
       [HO] => 2 
      ) 

     [2] => Array 
      (
       [ID_del_hotel] => 44 
       [Ocupacion] => QDP 
       [HO] => 3 
      ) 

    ); 
$i=0; 
foreach($arr as $value){ 
    $sql = "Select * from Tbl_name where ID_del_hotel=".$value['ID_del_hotel']." and HO>=".$value['Ho']; 
if(mysql_query($sql)){ 
    $resultArray[$i] = $value; 
    $i++; 
} 
} 
print_r($resultArray); 

$ resultArray的數組值與查詢匹配。

0

假設你的酒店ID是唯一的(應該是),你可以這樣做:

$arr = [ 
    [ 
     "ID_del_hotel" => 45, 
     "Ocupacion" => "SGL", 
     "HO" => 1 
    ], 
    [ 
     "ID_del_hotel" => 44, 
     "Ocupacion" => "DBL", 
     "HO" => 2 
    ], 
    [ 
     "ID_del_hotel" => 44, 
     "Ocupacion" => "QDP", 
     "HO" => 3 
    ] 
]; 

$ids = array_map(function ($a) { 
    return $a['ID_del_hotel']; 
}, $arr); 

$sql = 'SELECT * FROM table_name WHERE ID_del_hotel = (' . implode(',', $ids) . ')'; 

// output result 
var_dump($sql);