gpt4 book ai didi

mysql - 对 2 个表的 Eloquent 查询

转载 作者:行者123 更新时间:2023-11-30 22:12:40 25 4
gpt4 key购买 nike

我收到以下错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'value' in 'where clause' (SQL: select sum(gross) as value, first_name from clients left join transactions on clients.id = transactions.client_id where value > 0 group by first_name)

从这个函数?

$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;

最佳答案

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.

因此,请在下面的地方使用 sum(gross),而不是在其中使用 value

$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;

关于mysql - 对 2 个表的 Eloquent 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39547446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com