【问题标题】:How i can avoid the distinct() of watchdog?我如何避免看门狗的 distinct() ?
【发布时间】:2015-03-09 10:44:34
【问题描述】:

我想避免在我的代码中使用 distinct()。我需要一些帮助来创建一个表,该表将保留从 db 表看门狗中获取的值并检查是否已获取下一个值。如果下一个值在表中,则该值将不会插入表中...我需要真正的帮助,因为如果我解决了这个问题,我将结束我的模块。提前致谢!!

// Begin building the query.
  $query = db_select('watchdog', 'th')
    ->extend('PagerDefault')
    ->orderBy('wid')
    ->distinct()
    ->fields('th', array('variables', 'type', 'severity', 'message', 'wid'))
    ->limit(2000);

  // Fetch the result set.
  $result = $query  -> execute();

  // Loop through each item and add to $row.
  foreach ($result as $row) {
    blablablabla($row);
  }
}

我想要变量列的不同值。我不想要一个主题表,我只想要一个包含一些值的表。

【问题讨论】:

    标签: mysql database drupal drupal-7


    【解决方案1】:

    在循环之前创建一些空数组。在循环内部检查是否存在唯一值。如果它确实跳过了那个循环迭代(继续)。如果它没有将它存储在数组中并执行您的 blablabal();

        // query without distingc
    //  print_r($results); print out array of results to see what field to take as unique
    
          $already_processed = array();
    
      foreach ($result as $row) {
        $unique_value = $row['some_unique_field'];
        if (in_array($unique_value, $already_processed)) continue;
        $already_processed[] = $unique_value;
        blablabla($row);
      }
    

    类似的东西。

    【讨论】:

    • 我是 drupal 的新手,你能帮忙看看你说的话吗?
    • 这不是“drupal 的东西”,而是基本的 PHP。打印出 $row 变量并检查哪个字段适合使记录唯一。然后检查该字段并存储它,正如我上面解释的那样。
    • 我会做吗? $blabla = 数组(变量);越过循环?如果它存在,我将如何检查循环?我很困惑:'( :'(
    • 你去。这(可能)比从数据库查询中过滤结果要慢。
    • 你的意思是这样放 distinct() 会慢??
    猜你喜欢
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    相关资源
    最近更新 更多