简体   繁体   English

选择列总和等于Laravel中另一个表上另一列的值的所有行

[英]Select all rows where the sum of column equals a value of another column on another table in Laravel

Select all rows where the sum of column equals a value of another column on another table in Laravel 选择列总和等于Laravel中另一个表上另一列的值的所有行

purchases 购买

id | name               | amount
1  | Gamerzone Book     | 40
2  | Recipe Book        | 20
3  | EngineX Book       | 10

payments 付款方式

id  | purchase_id  | amount  
1     1              25       
2     1              15       
2     2              10   

Code

$query = DB::table('purchases')
        ->select(['purchases.id','purchases.name',
                   DB::raw("IFNULL(sum(payments.amount),0) as total")
            ])
         ->leftJoin('payments','payments.purchase_id','=','purchases.id')
         ->having('amount','=',DB::raw('total'));
         ->groupBy('purchases.id')->get();

When I run the code I get the following error Column not found: 1054 Unknown column 'amount' in 'having clause' 当我运行代码时,出现以下错误找不到列:1054“具有子句”中的未知列“金额”

Expected Result 预期结果

id | name               | total
1  | Gamerzone Book       40

I tried with all these but nothing seems to be working 我尝试了所有这些,但似乎没有任何效果

$query->where('purchases.amount','=',DB::raw('total'));
$query->having(DB::raw('purchases.amount'),'=',DB::raw('total'));
$query->havingRaw('IFNULL(sum(purchases.amount),0) = purchases.amount');
$paymentsQuery = DB::table('payments')->select(DB::raw("IFNULL(sum(payments.amount),0) as total"))->whereRaw('purchases.id = purchase_id')->groupBy('purchase_id');

$purchasesQuery = DB::table('purchases')->select(['id', 'name', 'amount', DB::raw('(' . $paymentsQuery->toSql() . ') as total')])->havingRaw('amount = total')->mergeBindings($paymentsQuery);

$result = $purchasesQuery->get();

$paymentsQuery will work as a subquery to get total for each purchase line. $ paymentsQuery将作为子查询来获取每个购买行的总计。

As a result we get sql like : 结果我们得到像这样的sql:

select `id`, `name`, `amount`, 
(select IFNULL(sum(payments.amount),0) as total from `payments` where purchases.id = purchase_id group by `purchase_id`) as total 
from `purchases` 
having amount = total

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL获取另一个表中所有行的计数,其中值等于当前表中的列 - MySQL get count of all rows in another table where the value equals column in current table MYSQL - 选择选择另一列等于某些内容的行 - MYSQL - Select selecting rows where another column equals something MySql 获取其中一列等于另一列等于另一表中的另一列的行 - MySql get rows where one column equals to another colum in another table where one column is equal 选择列等于另一个查询的任何值的位置 - Select where a column equals any value from another query 从表中选择行,其中具有相同id的另一个表中的行在另一列中具有特定值 - Select rows from a table where row in another table with same id has a particular value in another column 如何 select 一列具有其他列的最大值,以及另一个表中列的所有相应行? - How to select a column with max value of other column, and all corresponding rows of column in another table? 选择表中ID等于另一个表中另一个ID的行 - Select rows in a table where id is equals to another id in another table MySQL-选择列值仅为0的行,按另一列分组? - MySQL - Select rows where column value is only 0, group by another column? 选择laravel中价格总和等于或1000的所有行 - Select all rows where the sum of price equals or 1000 in laravel 选择所有数组值都出现在另一列中的所有行 - Select all rows where all array values are present in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM