【问题标题】:MySQL, PHP: Select * from table where id is not in arrayMySQL,PHP:从 id 不在数组中的表中选择 *
【发布时间】:2016-05-13 14:11:06
【问题描述】:

所以我目前有一个数据库表,我试图在其中选择所有记录,除了那些包含在我创建的数组中的记录。作为一些背景背景:

有问题的数据库表的结构是:

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

我将数据放入散列的 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

}

所以我要做的是创建一个 MySQL 查询,它从数组中读取 id ($id),并从中选择 * APART。通过查找,似乎我将不得不使用 'where not' 子句,但我不知道如何引用其中的哈希。

进一步澄清:我现在有一个提取数据的数组,如下所示:

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
}

我想从这个数组中获取 ID(例如 154)并选择表中没有任何上述 ID 的所有内容。我希望这有助于澄清?!

任何帮助将不胜感激!

【问题讨论】:

  • 你叫什么hash
  • $all[$server_id][$day] = $id; 抱歉,我想我可能使用了老板使用的术语! :P 在 PHP 中它可能被称为多维数组?但我认为它被方括号识别为哈希。
  • ??没有哈希。有了这个解释,你的目标变得更加不明确
  • 尝试提供一些您从中选择的记录集示例。和你想要得到的预期结果
  • 我认为你在混合概念,在 php 中哈希是 消息摘要,那么对你来说什么是哈希? xD

标签: php mysql arrays


【解决方案1】:

解决:

$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>";

从多维数组创建另一个数组以仅存储 id 并像 John Green 建议的那样使用 NOT IN。

谢谢!

【讨论】:

    【解决方案2】:

    如果 $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).")";
    

    在使用字符串连接编写查询时要小心。阅读SQL Injection 以及如何预防它。使用Prepared Statements 而不是纯串联。

    【讨论】:

      【解决方案3】:

      我想你只是想要一个 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 
      
      }
      

      【讨论】:

      • 请问如何定义 $id_array ? :)
      • 一个 PHP 的 ID 数组。如果您的 ID 是数字,例如 $id_array = array(1,5,12,24,33);
      猜你喜欢
      • 2012-03-16
      • 2012-01-10
      • 2016-09-23
      • 2023-03-24
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      相关资源
      最近更新 更多