convert to db_query and addExpression Is there a way convert this static query in a dynamic query?

$sql = "SELECT n.nid, n.title, n.created, n.type, n.status, IF((SELECT nid FROM `newsletter_lists` WHERE cid=$cid AND nid=n.nid), TRUE, FALSE) AS included FROM `node` AS n
  LEFT JOIN `newsletter_lists` AS nl ON n.nid = nl.nid";


This is what I have, but it throws errors.

$query = db_select('node', 'n')
  -&gt;fields('n', array('nid', 'title', 'created', 'type', 'status'))
  -&gt;extend('PagerDefault')
  -&gt;limit(5);

$subquery = db_select('newsletter_lists', 'snl')
  -&gt;fields('snl', array('nid'))
  -&gt;condition('cid', $cid)-&gt;condition('nid', `n.nid`)
  -&gt;execute();

$query-&gt;addExpression($subquery, 'included');

return $query-&gt;execute();
The problem is that addExpression() doesn't take a SelectQueryInterface as a parameter, but only a string:


  $expression: The expression string. May contain placeholders.


So the best you're going to be able to do is add the expression as a manual string. As the argument description suggests placeholders are fine so you can at least keep a bit of structure.

$query = db_select('node', 'n')
    -&gt;fields('n', array('nid', 'title', 'created', 'type', 'status'))
    -&gt;extend('PagerDefault')
    -&gt;limit(5);

$expression = 'IF((SELECT nid FROM `newsletter_lists` WHERE cid= :cid AND nid = n.nid), TRUE, FALSE)';

$query-&gt;addExpression($expression, 'included', array(':cid' =&gt; $cid));

return $query-&gt;execute();