Angus123
(Jack Sparrou)
January 11, 2014, 10:29am
1
Hello all Please help me to sum of column value in DB.
I have a some table in DB
id
id_user
temp_rating
stable_rating
I need withdraw sum of this value in CGridView, and sort by sum(temp_rating + stable_rating) By DESC, i don’t know how to make them
You can use CDbExpression for that.
For example,
$criteria->select = array(
'*',
new CDbExpression('SUM(temp_rating + stable_rating) AS sum_field'),
);
Also you’ll need to add a property $sum_field to your model, so that you can use $model->sum_field and so on.
Angus123
(Jack Sparrou)
January 11, 2014, 10:55am
3
You can use CDbExpression for that.
For example,
$criteria->select = array(
'*',
new CDbExpression('SUM(temp_rating + stable_rating) AS sum_field'),
);
Also you’ll need to add a property $sum_field to your model, so that you can use $model->sum_field and so on.
Hmmm… don’t work
public function actionTop(){
$criteria = new CDbCriteria();
$criteria->select = new CDbExpression('SELECT SUM(temp_rating + stable_rating) as sum_rating');
$criteria->limit = 100;
$criteria->order = 'sum_rating DESC';
$dataProvider = new CActiveDataProvider('Users', array(
'criteria'=>$criteria,
));
$this->render('top', array(
'dataProvider'=>$dataProvider,
));
}
view
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'showTableOnEmpty'=>false,
'columns'=>array(
'id',
'username',
'sum_rating',
),
'summaryText'=>false,
));
?>
Property sum_rating i’m add to model
Can you see the difference between
new CDbExpression('SUM(temp_rating + stable_rating)
and
new CDbExpression('SELECT SUM(temp_rating + stable_rating)
?
what exactly doesn’t work?
Is there any error?
what is the query generated (see logs)?
Angus123
(Jack Sparrou)
January 11, 2014, 3:35pm
7
No have an error
In log, just query
Querying SQL: SELECT SUM(temp_rating + stable_rating) AS `sum_rating` FROM
`tb_users` `t` LIMIT 10
in /var/www/vlad/protected/views/site/top.php (11)
in /var/www/vlad/protected/controllers/SiteController.php (38)
in /var/www/vlad/index.php (13)
Error no, just no output.
Controller
public function actionTop(){
$criteria = new CDbCriteria;
$criteria->select = array(
'*'=> new CDbExpression('SUM(temp_rating + stable_rating) AS `sum_rating`'),
);
//$criteria->limit = 100;
//$criteria->order = 'sum_rating DESC';
$dataProvider = new CActiveDataProvider('Users', array(
'criteria'=>$criteria,
));
$this->render('top', array(
'dataProvider'=>$dataProvider,
));
}
but , strange, strange that I have a database 2 record, and the widget generates two rows in the table, but they are empty
Angus123:
but , strange, strange that I have a database 2 record, and the widget generates two rows in the table, but they are empty
That’s because you did array(’’ => CDbExpression) instead of array(’ ’, CDbExpression)
Angus123
(Jack Sparrou)
January 11, 2014, 4:17pm
9
Yes, it’s work Thank you. But its a little somethink… I need to sum a column for a current user(in first post i’m write a structure database), now script calculate a summary sum of all users
What’s SQL query for this?
Angus123
(Jack Sparrou)
January 11, 2014, 5:22pm
11
I need to execute next query, but in CActiveDataProvider:
SELECT SUM(temp_rating + stable_rating) as 'sum_rating' WHERE user_id = '$data->id'
Where $data->id, its id of data in DB
So in terms of SQL your final query should look like this:
SELECT user_table.*, SUM(…) FROM user_table LEFT JOIN some_table ON some_table.user_id = user_table.id GROUP BY some_table.user_id
Right?
Angus123
(Jack Sparrou)
January 11, 2014, 6:05pm
14
I found solution
Controller
public function actionTop(){
$criteria = new CDbCriteria;
$criteria->limit = 100;
$criteria->order = new CDbExpression('temp_rating + stable_rating DESC');
$dataProvider = new CActiveDataProvider('Users', array(
'criteria'=>$criteria,
));
$this->render('top', array(
'dataProvider'=>$dataProvider,
));
}
view
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'showTableOnEmpty'=>false,
'columns'=>array(
'id',
'username',
'sum_rating'=>array(
'name'=>'sum_rating',
'value'=>'$data->temp_rating + $data->stable_rating',
),
),
'summaryText'=>false,
));
?>
And it work good Thank ORey for helping