简体   繁体   English

MySQL,PHP:从表中选择*,其中id不在数组中

[英]MySQL, PHP: Select * from table where id is not in array

so I currently have a database table where I am trying to select all of the records EXCEPT for those that are included in an array that I have made. 所以我目前有一个数据库表,我试图选择所有记录除了那些包含在我已经制作的数组中的记录。 As some background context: 作为一些背景背景:

The structure of the database table in question is: 有问题的数据库表的结构是:

server_status:
id int(11)
server_id int(11)
time_checked datetime
status char(1)

My PHP script to get the data into the hash looks like this: 将数据导入哈希的PHP脚本如下所示:

$sql2 = "SELECT server_id, time_checked,id from server_status where   time_checked<'$date' order by server_id;";
$result2=$conn->query($sql2);
while($row2 = $result2->fetch_assoc()){

 $server_id = $row2['server_id'];
 $id = $row2['id'];
 $dt = $row2['time_checked'];

 $year = substr($dt,0,4);
 $month = substr($dt,5,2);
 $day = substr($dt,8,2);

 $day = "$year-$month-$day";

 $all[$server_id][$day] = $id;  // ARRAY

}

So what I'm trying to do is create a MySQL query that reads in the ids ($id) from the array, and selects * APART from that. 所以我要做的是创建一个MySQL查询,从数组中读取id($ id),并从中选择* APART。 From looking it up, it seems like I will have to use a 'where not' clause, but I do not know how to reference the hash in this. 从查找起来,似乎我将不得不使用'where not'子句,但我不知道如何引用哈希。

Further clarification: I now have an array which extracts data which looks like this: 进一步澄清:我现在有一个数组,它提取如下所示的数据:

1{
2016-05-05 : 252
2016-05-10 : 406
2016-04-27 : 141
2016-05-04 : 164
2016-05-09 : 263
2016-05-03 : 153
2016-04-26 : 131
2016-04-14 : 1
2016-04-18 : 31
2016-04-21 : 111
2016-04-20 : 61
2016-04-19 : 51
2016-04-15 : 21
2016-04-25 : 121
}
2{
2016-05-10 : 452
2016-05-05 : 198
2016-05-09 : 264
2016-05-04 : 165
2016-04-26 : 132
2016-04-27 : 143
2016-04-25 : 122
2016-04-21 : 112
2016-05-03 : 154
}

I want to take the IDs from this array (eg 154) and select everything in the table which doesn't have any of the above IDs. 我想从这个数组中获取ID(例如154)并选择表中没有任何上述ID的所有内容。 I hope this helps to clarify?! 我希望这有助于澄清?!

Any help will be greatly appreciated! 任何帮助将不胜感激!

In case $all is the array you want to extract the unwanted ids from, this might be what you need after the code you provided: 如果$all是要从中提取不需要的ID的数组,这可能是您提供的代码后所需的内容:

$ids_to_exclude = array();

// iterate through servers
foreach ($all as $server_id => $dates) {
    // iterate through dates of each server
    foreach ($dates as $date => $id) {
        // If a value is not in the array, add it.
        // In case ids don't repeat, you won't need this if
        if (!in_array($id, $ids_to_exclude)) {
             // add $id to the array
             $ids_to_exclude[] = $id;
        }
    }
}

$sql_condition = "where `id` not in (".implode(",",$ids_to_exclude).")";

Just be careful when writing queries with string concatenation. 使用字符串连接编写查询时要小心。 Read about SQL Injection and how to prevent it. 阅读有关SQL注入以及如何防止它的信息。 Use Prepared Statements instead of pure concatenation. 使用准备语句而不是纯连接。

I think you just want a NOT IN, right? 我想你只想要一个NOT IN,对吗?

$sql2 = "SELECT id, server_id, time_checked,id 
         FROM server_status 
         WHERE 
            id NOT IN (".implode(',', $id_array)."
            AND time_checked<'$date' 
         ORDER BY server_id;";

$result2=$conn->query($sql2);

while($row2 = $result2->fetch_assoc()){   
   $server_id = $row2['server_id'];
   $id = $row2['id'];
   $dt = date('Y-m-d', strtotime($row2['time_checked']));
   $all[$server_id][$dt] = $id;  // ARRAY 

}

SOLVE: 解决:

$sql2 = "SELECT server_id, time_checked,id from server_status where time_checked<'$date' order by server_id;";
$result2=$conn->query($sql2);
while($row2 = $result2->fetch_assoc()){


 while($row2 = $result2->fetch_assoc()){
    $server_id = $row2['server_id'];
    $id = $row2['id'];
    $dt = date('Y-m-d', strtotime($row2['time_checked']));
    $all[$server_id][$dt] = $id;  // ARRAY
 }
}

 $stack = array();
  $keys = array_keys($all);
  for($i = 0; $i < count($all); $i++) {
      foreach($all[$keys[$i]] as $key => $value) {
        array_push($stack, $value);
      }
  }


$ids = join(',',$stack);
$sql = "SELECT * FROM server_status WHERE time_checked<'$date' AND id NOT IN ($ids)";
$result=$conn->query($sql);
echo "Server status data has been deleted.<br>";

Created another array from the multi-dimensional array to store the ids only and use NOT IN like John Green suggested. 从多维数组中创建另一个数组以仅存储id,并使用像John Green建议的NOT IN。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM