Reputation: 61
I have table from mysql like below:
id | name | grade | k1 | k2 | k3 | s1| s2 | s3| e1 | e1 | e3 |
1 | Aa | 5 | 1 | 0 | 5 | 3 | 2 | 1 | 0 | 6 | 1 |
2 | Bb | 1 | 1 | 3 | 5 | 3 | 5 | 3 | 4 | 6 | 1 |
3 | Cc | 2 | 1 | 4 | 2 | 2 | 2 | 4 | 0 | 6 | 1 |
4 | Dd | 4 | 1 | 3 | 5 | 3 | 3 | 1 | 0 | 6 | 1 |
5 | Ee | 3 | 1 | 5 | 2 | 1 | 0 | 5 | 0 | 6 | 1 |
6 | Ff | 2 | 1 | 3 | 1 | 3 | 4 | 2 | 0 | 6 | 1 |
7 | Gg | 5 | 1 | 1 | 5 | 5 | 2 | 1 | 0 | 6 | 1 |
Using FOR and FOREACH looping, i do able to show all row. But in final view/table i want to
SUM [k1,k2,k3], SUM[s1,s2,s3] and SUM[e1,e2,e3]
So it will place new colum as K, S and E in each row.
Here are my code:
for ($i = 0; $i < Evaluation::model()->count(); $i++) {
foreach (Yii::app()->db->createCommand()
->from('evaluation')
->queryAll() as $item) {
// Row-Column start here
// id | name | grade | K | S | E |
}
}
thanks.
Upvotes: 0
Views: 181
Reputation: 1341
I love the query builder. So here is one of the solution.
$result = Yii::app()->db->createCommand()->
select('id, name, grade, (k1+k2+k3) AS K, (s1+s2+s3) AS S, (e1+e2+e3) AS E')->
from(MyModel::model()->tableName())->
queryAll();
var_dump($result);
Upvotes: 0
Reputation: 409
select k1+k2+k3, s1+s2+s3, e1+e2+e3 from t
and it is not yii question, it is about MySql
If you want to express it in Yii, you can
Yii:app()->createCommands()
->select('k1+k2+k3, s1+s2+s3, e1+e2+e3')
->from('t')
->queryAll();
Upvotes: 0