PHP SQL查询其中IN数组的数组

时间:2017-05-18 04:06:11

标签: php sql arrays

如何运行SQL查询:
选择字段ID_del_hotelOcupacion以及HO > = 匹配变量IN Array[ID_del_hotel, Ocupacion, HO]

$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
        )

)

3 个答案:

答案 0 :(得分:0)

循环遍历数组以构建ID为

的IN子句

的MySQL

WHERE db_id IN (45,46,47)

答案 1 :(得分: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具有与查询匹配的数组值。

答案 2 :(得分: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);