微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel 非分组字段 'net_total' 用于 HAVING 子句

如何解决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_iddoc_type 字段,分别填充为 Apps\Models\Invoiceinvoice_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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?