如何解决Laravel 非分组字段 'net_total' 用于 HAVING 子句
我编写了一个在本地服务器上运行良好的查询。但是在生产中,它不起作用。这是查询。
Invoice::with('contact_fk','transactions')
->where('date','<=',$this->query_date)
->withSum('transactions','amount')
->havingRaw('COALESCE(transactions_sum_amount,0) < invoices.net_total')
->orderBy('due_date','asc');
错误如下:
sqlSTATE[42000]: Syntax error or access violation: 1463 Non-grouping field
'net_total' is used in HAVING clause (sql: select sum(`net_total`) as
aggregate from (select `invoices`.*,(select sum(`transaction_allocations`.`amount`)
from `transaction_allocations` where `invoices`.`id` = `transaction_allocations`.`doc_id`
and `transaction_allocations`.`doc_type` = App\Models\Invoice and
`transaction_allocations`.`deleted_at` is null) as `transactions_sum_amount` from
`invoices` where `date` <= 2021-07-27 having COALESCE(transactions_sum_amount,0) < net_total)
as `temp_table`)
网上的很多建议都围绕着制作'strict' => false
而展开,这是不可接受的,因为它避免了问题并且没有解决问题。
我该如何解决?我的要求是带上所有未付的发票。付款存储在名为 transactions
的单独表中。因此,所有发票的 net_total
大于此发票的 amount
交易总和。
根据要求,包括带有相关字段的两个表结构。它们具有多态关系。 transaction_allocation
表包含 doc_id
和 doc_type
字段,分别填充为 Apps\Models\Invoice
和 invoice_id
。关系的名称是 transactions
。
Schema::create('invoices',function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->decimal('net_total',19,4);
});
Schema::create('transaction_allocations',function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('transaction_id')->nullable()->constrained();
$table->Nullablemorphs('doc');
$table->decimal('amount',4)->nullable();
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。