SQL Query Builder

Hi guys,

From below sql query, how to write in CDbCommand. Pls help.




SELECT id,qty_balance FROM product WHERE id IN (SELECT MAX(id) FROM product WHERE company_id=2 GROUP BY product_id)



Thanks.


$user = Yii::app()->db->createCommand()

->select('id, qty_balance')

->from('product ')

->where('in','id',<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />))      //----->(SELECT MAX(id) FROM product WHERE company_id=2 GROUP BY product_id)

->queryAll();

The condition where can be an array or a string, in your case I would do:




$users=Yii::app()->db->createCommand()

->select('id,qty_balance')

// allways use parameters --- avoid some malicious attacks

->where('id IN (SELECT MAX(id) FROM product WHERE company_id=:company_id GROUP BY product_id)',array(':company_id'=>2))

->queryAll();



thanks Antonio Ramirez’s solution. =)