我有一个包含 3 个字段(id、jobsprocess、percent)的表,并且想要即时计算出累积百分比,因此它是一个派生字段。
在 sql 中我会这样做,它返回 13 条记录。
set @x := 0;
SELECT ID, jobsprocess, PERCENT, (@x := @x + PERCENT) AS cumaltive
FROM `hdb`.`lookupprocess`
inner join jobsprocess on jobprocess = process_id
where projid = 1302035
order by id
我如何在 yii 中做到这一点?到目前为止,我有以下内容,但它没有给我想要的结果
$lastrun = Yii::app()->db->createCommand("
SELECT ID, jobsprocess, PERCENT , (:criteria = :criteria + PERCENT) AS cumulative
FROM `hdb`.`lookupprocess`
inner join jobsprocess on jobprocess = process_id
where projid = $projid
order by id " )->queryAll(true, array(':criteria'=>0));
返回 13 条记录,但累计始终为 0。
我也尝试过以下方法,但出现错误
$user = Yii::app()->db->createCommand()
->select('ID, jobsprocess, PERCENT , (:zero := (:zero + PERCENT)) AS cumulative')
->from('lookupprocess')
->join('jobsprocess','jobprocess = process_id')
->where('projid = :projid', array(':projid'=>$projid,':zero'=>0))
->queryAll();
它:zero
作为一个字符串。
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':= ('0' + PERCENT)) AS cumulative
FROM `lookupprocess`
JOIN `jobsprocess` ON job' at line 1. The SQL statement executed was: SELECT ID, jobsprocess, PERCENT , (:zero := (:zero + PERCENT)) AS cumulative
FROM `lookupprocess`
JOIN `jobsprocess` ON jobprocess = process_id
WHERE projid = :projid