不在PHP中的SQL等效运算符

时间:2019-07-17 18:34:56

标签: php sql operators

PHP中的SQL上是否存在等效于NOT IN的运算符?

if(count($result1) > 0) {
    $query_transline_conflict = "SELECT * FROM transaction_line as tl LEFT JOIN transaction as t ON tl.transaction_id = t.transaction_id LEFT JOIN resources as r ON tl.resource_id = r.resource_id WHERE t.equipment_class_id = '$equipment_class_id' AND tl.returned = '0'";
    $result_transline_conflict = mysqli_query($conn, $query_transline_conflict);
    $result2 = mysqli_fetch_all($result_transline_conflict,MYSQLI_ASSOC); 
    $x = 0;
       foreach($result2 as $row){
         $already_booked_range = getDatesFromRange($row['start_date'],$row['end_date']);
         $new_array_already_booked[$x] = $already_booked_range;
         $x++;
  }

我输入的是开始日期和结束日期。现在$new_array_already_booked存储将要比较的日期范围。所以我想对PHP使用NOT IN运算符进行比较。我希望我有道理。还是一个初学者并尝试学习。

Array
(
[0] => Array
    (
        [0] => 2019-07-25
        [1] => 2019-07-26
        [2] => 2019-07-27
    )

[1] => Array
    (
        [0] => 2019-07-30
        [1] => 2019-07-31
        [2] => 2019-08-01
    )

)

2 个答案:

答案 0 :(得分:2)

要检查元素是否在数组中,可以使用in_array()

$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
if (in_array("mac", $os)) {
    echo "Got mac";
}

对于NOT IN,您可以使用!in_array()

$os = array("Mac", "NT", "Irix", "Linux");
if (!in_array("MyValue", $os)) {
    echo "MyValue not in array";
}

https://www.php.net/manual/en/function.in-array.php

答案 1 :(得分:2)

如果要检查一个数组中的任何元素是否在另一个数组中,可以使用array_intersect

$a = Array('bird', 'butterfly', 'bat');
$b = Array('ball', 'helmet', 'bat');

if (!array_intersect($a, $b)) echo 'not in';