2个表上的雄辩查询

时间:2016-09-17 13:36:08

标签: mysql laravel eloquent

我收到以下错误:

  

SQLSTATE [42S22]:未找到列:1054未知列'值'在   ' where where' (SQL:选择sum(gross)作为值,first_name来自   clients transactions上的clients已加入idtransactions =   client_idvalue其中first_name> 0分组$data = DB::table('clients')->leftjoin('transactions','clients.id','=','transactions.client_id') ->select(DB::raw('sum(gross) as value, first_name')) ->where('value','>',0) ->groupBy('first_name') ->get(); return $data;

从这个功能?

{{1}}

1 个答案:

答案 0 :(得分:0)

  

从右到左向后计算SQL。因此,在select子句之前解析和评估where子句。因此,尚未发生总和(总)与值的混叠。

     

别名可以在GROUP BY,ORDER BY或HAVING子句中使用。

所以请不要在value中使用sum(gross),而是在下面的地方使用$data = DB::table('clients')->leftjoin('transactions','clients.id','=','transactions.client_id') ->select(DB::raw('sum(gross) as value, first_name')) ->where('sum(gross)','>',0) ->groupBy('first_name') ->get(); return $data;

Problem 1
    - yiisoft/yii2-mongodb 2.1.1 requires ext-mongodb >=1.0.0 -> the requested PHP extension mongodb is missing from your system.
    - yiisoft/yii2-mongodb 2.1.0 requires ext-mongodb >=1.0.0 -> the requested PHP extension mongodb is missing from your system.
    - Installation request for yiisoft/yii2-mongodb ~2.1.0 -> satisfiable by yiisoft/yii2-mongodb[2.1.0, 2.1.1].