1

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 です。

次の3eも試しましたが、エラーが発生します

$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 
4

1 に答える 1

1

クエリの FROM 部分で SQL 変数を宣言できます。

SELECT ID, jobsprocess, PERCENT, (@x := @x + PERCENT) AS cumaltive
FROM `hdb`.`lookupprocess`, (SELECT @x=0) q
inner join jobsprocess on jobprocess = process_id
where projid = 1302035
order by id

したがって、次のようなものです。

$lastrun = Yii::app()->db->createCommand("
    SELECT ID, jobsprocess, PERCENT , (@x := @x + PERCENT) AS cumulative
    FROM `hdb`.`lookupprocess`, (SELECT @x:=0) q
    inner join jobsprocess on jobprocess = process_id
    where projid = $projid
    order by id " )->queryAll();
于 2013-05-09T21:25:01.890 に答える