我有两个表Products(id, name)
和Views(id,count,time)
,这两个表彼此无关。这是我的代码:
$dbCommand = Yii::app()->db->createCommand("
SELECT P.`id`, P.`name`, V.`time`
FROM `products` P, `views` V
WHERE P.`type` = 2
ORDER BY V.`time` DESC
");
$data = $dbCommand->queryAll();
它正在运行,但我想将此查询转换为CDbCriteria语法。
$cdb = new CDbCriteria();
$cdb->select = //???
$cdb->where = //???
$cdb->order = //???
我该怎么做?有人可以帮帮我吗?
答案 0 :(得分:2)
您无法使用CDbCriteria,请尝试使用查询构建器。
Yii::app()->db->createCommand()
->select('P.id, P.name, V.time')
->from('products P, views V')
->where('P.type = :type')
->order('V.time DESC')
->queryAll(array(
':type' => 2
));