dpuuys2198
dpuuys2198
2014-10-07 10:36

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

已采纳

How can I convert this query using db_select() method?

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

Tried using addExpression and countQuery() etc.,

$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();
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douchiwan1503 douchiwan1503 7年前

    Found a solution from the drupal community. Not sure about if this is the exact method, Anyway I got the expected output using the following code

    $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();
    
    点赞 评论 复制链接分享