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();
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();