i tried this

$q_obj->addExpression('IF(b.uid1<>:f,b.uid1,b.uid2)', 'to', array(':f'=>3));

but it gives error , despite the fact that this is the correct SQL query.

(SELECT id as id, IFNULL(c.uid1<>3,'c.uid1') as uid FROM table1 AS c WHERE c.id = 28)

Comments

pasqualle’s picture

Version: 7.15 » 7.x-dev
Assigned: b8x » Unassigned
Category: task » support
Status: Active » Closed (fixed)

addExpression() with IF statement example. Returns node status for page type, return node title otherwise:

$query = db_select('node', 'n');
$query->fields('n', array('nid'));
$query->addExpression('IF(n.type = :type, n.status, n.title)', 'myfield', array(':type' => 'page'));
print_r($query->execute()->fetchAllKeyed());