简体   繁体   English

PHP中SQL字段的总和

[英]Sum of SQL fields in PHP

Hi I'm trying to find the sum of SQL fields in PHP. 嗨,我正在尝试查找PHP中SQL字段的总和。 This sum must be less than or equal to the number which I receive in controller from JavaScript. 此总和必须小于或等于我在JavaScript中从控制器收到的数字。 But my SQL syntax is not working in PHP, instead it fetches all rows from the database. 但是我的SQL语法在PHP中不起作用,而是从数据库中获取所有行。

public function feedbackaverageAction(){                
    $value =$_GET['value'];
    $db = Zend_Registry::get('dbadapter');
    $select = new Zend_Db_Select($db);
    $select = $db->select();
    $select->from(array('feedback' => 'feedback'),
                   array ('customdata',
                          'checkinprocess',
                          'service_by_owner',
                          'locationrating',
                          'cleanlinessrating',
                          'valueformoneyrating',
                          'kitchenequipmentrating'));
    $select->where('feedback.checkinprocess' + 
                   'feedback.service_by_owner' + 
                   'feedback.locationrating' +
                   'feedback.cleanlinessrating' +
                   'feedback.valueformoneyrating' +
                   'feedback.kitchenequipmentrating' <= $value);
    $stmt = $select->query();
    $result = $stmt->fetchAll();
    $this->view->rows = $result;
}

Can anyone help me how to find the sum and check it to the value which I got in the controller. 任何人都可以帮助我如何找到总和并将其检查为我在控制器中获得的值。

Try this code and make SQL query like this. 尝试使用此代码,使SQL查询像这样。

$value = $_GET['value'];
$db = Zend_Registry::get('dbadapter');
$select = new Zend_Db_Select($db);
$select = $db->select();

$select->from(array('feedback' => 'feedback'),array ('customdata','checkinprocess','service_by_owner','locationrating','cleanlinessrating','valueformoneyrating','kitchenequipmentrating'));

$select->where(new Zend_Db_Expr('feedback.checkinprocess'+'feedback.service_by_owner'+'feedback.locationrating'+'feedback.cleanlinessrating'+'feedback.valueformoneyrating'+'feedback.kitchenequipmentrating')<= $value);

$stmt = $select->query();

$result = $stmt->fetchAll();

$this->view->rows = $result;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM