【问题标题】:How can I convert the query using Drupal `db_select()` method?如何使用 Drupal `db_select()` 方法转换查询?
【发布时间】:2014-10-07 10:36:53
【问题描述】:

如何使用db_select() 方法转换此查询?

select id, name, (select count(*) from da_pages a where b.id = a.page_id) count, active from da_pages b where page_id=$arg

尝试使用addExpressioncountQuery()等,

$query = db_select('da_pages', 'dp')
      ->fields('dp',array('id','name','active'))
      ->condition('page_id', $arg,'=')
      ->countQuery()
      ->extend('PagerDefault')
      ->limit(5); // line1
      //$query->addExpression('count(select * from da_pages a where db.id = a.page_id)', 'count');
  $result = $query->execute();

【问题讨论】:

    标签: php mysql drupal drupal-7


    【解决方案1】:

    从 drupal 社区找到了一个解决方案。不确定这是否是确切的方法,无论如何我使用以下代码得到了预期的输出

    $count = "(select count(*) from da_pages a where dp.id = a.page_id)";
      $query = db_select('da_pages', 'dp')
          ->fields('dp',array('id','name','active'))
          ->condition('page_id', $arg,'=')
          ->extend('PagerDefault')
          ->limit(5); // line1
      $query->addExpression($count, 'count');
      $result = $query->execute();
    

    【讨论】:

      猜你喜欢
      • 2018-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      相关资源
      最近更新 更多