Reputation: 2370
Im getting the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'value' in 'where clause' (SQL: select sum(gross) as value, first_name from
clients
left jointransactions
onclients
.id
=transactions
.client_id
wherevalue
> 0 group byfirst_name
)
From this function?
$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;
Upvotes: 1
Views: 74
Reputation: 5246
SQL is evaluated backwards, from right to left. So the where clause is parsed and evaluate prior to the select clause. Because of this the aliasing of sum(gross) to value has not yet occurred.
Aliases can be used in GROUP BY, ORDER BY, or HAVING clauses.
So instead of using value
in where please use sum(gross)
in where like below.
$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;
Upvotes: 0