Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在SQL中计算结果的最佳方法是什么?_Mysql_Sql_Postgresql_Db2 - Fatal编程技术网

Mysql 在SQL中计算结果的最佳方法是什么?

Mysql 在SQL中计算结果的最佳方法是什么?,mysql,sql,postgresql,db2,Mysql,Sql,Postgresql,Db2,我寻找最佳实践,以改进模型中的美国应用(关于MCV模式) 对于很少的数据,我们可以根据SQL请求或模型进行处理 Ex:agregate-data-on-SQL(我的agregateresult在别名:result\u-year上) 我的模型可以这样定义(phpfriendly): 或者我想用其他方法处理: Ex:从我的SQL中获取所有数据,并在模型上放弃(在函数get my value中) 这段代码只是用来解释我的问题:我在哪里搜索创建我的应用程序的最佳方法 对于agregate数据,SQL总

我寻找最佳实践,以改进模型中的美国应用(关于MCV模式)

对于很少的数据,我们可以根据SQL请求或模型进行处理

Ex:agregate-data-on-SQL(我的agregateresult在别名:result\u-year上)

我的模型可以这样定义(phpfriendly):

或者我想用其他方法处理:

Ex:从我的SQL中获取所有数据,并在模型上放弃(在函数get my value中)

这段代码只是用来解释我的问题:我在哪里搜索创建我的应用程序的最佳方法

  • 对于agregate数据,SQL总是比模型(PHP或其他语言)更快吗
  • 在什么情况下,模型(PHP或其他)中的计算比SQL更好
  • 有什么好的做法可以将其SQL恢复为模型吗
PS:这不是一个带有PHP标签的问题,因为这不是一个关于PHP的问题,而是关于所有语言(PHP/JAVA/ASP/C#……等)模型中的SQL的问题


非常感谢

一般来说,SQL被设计为在聚合数据上运行良好

此外,如果只传递聚合数据而不是所有数据来构建聚合,则网络的使用率会降低


因此,如果可能,请让sql引擎聚合数据并将其提取为聚合数据。相反,如果应用服务器(java、php或其他)上的内存中已经有所有数据,则可以直接在内存中聚合数据。

Hi!谢谢你的回复!你知道有没有一个网站可以解释所有这些规则?(我不知道我们是否可以删除内存中的所有数据?)
SELECT
  id AS product_id,
  name AS product_name,
  ISNULL(quantity_january, 0) + ISNULL(quantity_february, 0) + ISNULL(quantity_march, 0) as result_year
FROM
  product_sale
class productResult{
    // define one var by sql column.
    $this->resultYear = null;

    function productResult(){
        //here I make the link between SQL and PHP.
    }

    // define function for get the sql result.
    public function getResultYear(){
        return $this->resultYear;
    }
}

// I get just the my result.
$productResult->getResultYear();
SELECT
  id AS product_id,
  name AS product_name,
  quantity_january,
  quantity_february
  quantity_march
FROM
  product_sale


class productResult{
    // define one var by sql column.
    $this->quantityJanuary= null;
    $this->quantityFebruary= null;
    $this->quantityMarch= null;

    function productResult(){
        //here I make the link between SQL and PHP.
    }

    // define function for get the sql result.
    public function getResultYear(){
        return $this->quantityJanuary
             +$this->quantityFebruary
             +$this->quantityMarch;
    }
}
// I get just my resulyt.
$productResult->getResultYear();