简体   繁体   English

将两个查询合并为一个查询Laravel

[英]Combine two queries into one query Laravel

I need to combine two queries into one so i can call it one time on my foreach inside a table in my blade view. 我需要将两个查询合并为一个,以便可以在刀片视图中的表中的foreach上调用一次。 I tried using merge() but it displays two separate queries. 我尝试使用merge()但它显示两个单独的查询。 CountofPatents are all the patents that were submitted by a user and CountofApprovedPatents are the patents which has the status of "2". CountofPatents是用户提交的所有专利, CountofApprovedPatentsstatus为“ 2”的专利。 Here's how it looks now: 现在是这样的:

array 0 [person_id : 1 数组0 [person_id:1
CountofApprovedPatents: 4 批准专利数:4
status: 2] 状态:2]

array 1 [person_id : 2 数组1 [person_id:2
CountofApprovedPatents: 2 批准专利数:2
status: 2] 状态:2]

array 2 [person_id : 1 数组2 [person_id:1
CountofPatents: 5] 专利数:5]

array 3 [person_id : 2 数组3 [person_id:2
CountofPatents: 4] 专利数:4]

Here's how it's supposed to look like 这是应该的样子

array 0 [person_id : 1 数组0 [person_id:1
CountofApprovedPatents: 4 批准专利数:4
CountofPatents: 5] 专利数:5]

array 1 [person_id : 2 数组1 [person_id:2
CountofApprovedPatents: 2 批准专利数:2
CountofPatents: 4] 专利数:4]

Here's my code so far 到目前为止,这是我的代码

AdminController AdminController

$start = $request->input('start');
$end = $request->input('end');
$approvedPatents = DB::table('patents')
->select('person_id', DB::raw('count(*) as CountofApprovedPatents'))
->where([
    ['status', '=', 2],
    ['date_approved', '>=', $start],
    ['date_approved', '<=', $end],
])
->groupBy('person_id')
->orderBy('status', 'desc')
->get();

$totalPatents = DB::table('patents')
->select('person_id', DB::raw('count(*) as CountofPatents'))
->where([
    ['date_approved', '>=', $start],
    ['date_approved', '<=', $end],
])
->groupBy('person_id')
->orderBy('status', 'desc')
->get();  

$patents = $approvedPatents->merge($totalPatents);
$results = $patents->all();  
return view('admin.patents.showPatents', compact('results', 
'start', 'end'));

Is it possible to combine these two queries? 是否可以合并这两个查询?

$results = DB::table('patents')
    ->union($approvedPatents)
    ->union($totalPatents)
    ->get();

What I think you are looking for is Unions . 我认为您正在寻找的是工会

It'll allow you to specify a query and then join it with the final query. 它允许您指定一个查询,然后将其与最终查询连接。

Eg. 例如。

$first = DB::table('users')
            ->whereNull('first_name');

$users = DB::table('users')
            ->whereNull('last_name')
            ->union($first)
            ->get();

Try: 尝试:

$approvedPatents = DB::table('patents')
->select('person_id', DB::raw('count(*) as CountofApprovedPatents, status'))
->where([
    ['status', '=', 2],
    ['date_approved', '>=', $start],
    ['date_approved', '<=', $end],
])
->groupBy('person_id')
->orderBy('status', 'desc')


$totalPatents = DB::table('patents')
->select('person_id', DB::raw('count(*) as CountofPatents'))
->where([
    ['date_approved', '>=', $start],
    ['date_approved', '<=', $end],
])
->groupBy('person_id')
->orderBy('status', 'desc')
->union($approvedPatents)
->get();  

Otherwise you could be looking for Joins - worth a look at the docs :) 否则,您可能正在寻找Joins-值得看看文档:)

Unions: https://laravel.com/docs/5.8/queries#unions 联盟: https //laravel.com/docs/5.8/queries#unions

Joins: https://laravel.com/docs/5.8/queries#joins 加入: https //laravel.com/docs/5.8/queries#joins

You can use MySQL's IF condition in your raw selection of columns. 您可以在原始的列选择中使用MySQL的IF条件

$results = DB::table('patents')
->select('person_id', 
          DB::raw('sum(IF(status = 2,1,0)) as CountofApprovedPatents'),
          DB::raw('count(*) as CountofPatents')
->where([
    ['date_approved', '>=', $start],
    ['date_approved', '<=', $end],
])
->groupBy('person_id')
->orderBy('status', 'desc')
->get();

The above IF condition in sum() adds 1 to the result if current row's status is 2 , else it adds 0 which effectively doesn't break the sum. 上述IF条件sum()1到结果,如果当前行的status2 ,否则它添加0从而有效地不破的总和。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM