如何使用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
尝试使用addExpression
和countQuery()
等,
$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();
答案 0 :(得分:0)
从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();