gpt4 book ai didi

php - laravel5 中 where 条件内的两个子选择查询?

转载 作者:行者123 更新时间:2023-11-29 22:26:29 24 4
gpt4 key购买 nike

我想比较 Laravel5 中查询的 where 条件内的两个选择查询,

下面给出了我在laravel中查询的where条件,但它不是wokring。

即,从发票中选择其中(选择查询1)==(选择查询2)

 $invoice->orWhere(function($query) {
$query->where('invoices.status', '=', InvoiceStatusConstant::SENT)
->where('invoices.due_date', '<', Carbon::today()->toDateString())
->where(DB::raw('(SELECT ROUND(SUM(invoice_items.quantity*invoice_items.rate), 2) from invoice_items'), '=',
DB::raw("(SELECT SUM(payments.amount) from payments "));
});

如何解决这个问题?

最佳答案

DB::raw 不会给你一个值。这只是创建表达式的一种方法。如果你想这样做,你应该使用:

DB::select(DB::raw("(SELECT SUM(payments.amount) from payments"))

但这会给你一个数组。因此,我会执行以下操作:

DB::select(DB::raw("(SELECT SUM(payments.amount) as pay from payments"))[0]->pay

甚至更简单

DB::select("(SELECT SUM(payments.amount) as pay from payments")[0]->pay

关于php - laravel5 中 where 条件内的两个子选择查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30231324/

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