How to use if in select condition

Hi All,

I got a query that involve a IF condition to select which field i should apply in the SUM and i wish to write it in AR format, can anyone give me some idea?

Original query is like this


$buyback_prod_processing_list_qty_select_sql = “SELECT sum(IF(brg.buyback_request_group_site_id > 0, br.buyback_quantity_confirmed, br.buyback_request_quantity)) as first_list_qty_total FROM " . TABLE_BUYBACK_REQUEST_GROUP . " AS brg INNER JOIN " . TABLE_BUYBACK_REQUEST . " AS br ON brg.buyback_request_group_id = br.buyback_request_group_id WHERE br.products_id = '” . tep_db_input($products_id) . “'AND brg.buyback_status_id = 2 AND br.buyback_quantity_received=0”;


I try to write like below and it have error


$criteria=new CDbCriteria;

$criteria->select =‘SUM(IF(buyback_request_group.buyback_request_group_site_id > 0, t.buyback_quantity_confirmed, t.buyback_request_quantity)) as first_list_qty_total’;

$criteria->with = array(‘buyback_request_group’=>array(‘condition’=>‘buyback_status_id = “2”’));

$criteria->params = array(’:productId’=>$productId);

$result = $this->model()->find($criteria);


And the error message is

invalid column "SUM(IF(buyback_request_group.buyback_request_group_site_id > 0".

The system assume it was a field , can anyone help me on this?

Have you tried like here?