1

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. I tried using merge() but it displays two separate queries. CountofPatents are all the patents that were submitted by a user and CountofApprovedPatents are the patents which has the status of "2". Here's how it looks now:

array 0 [person_id : 1
CountofApprovedPatents: 4
status: 2]

array 1 [person_id : 2
CountofApprovedPatents: 2
status: 2]

array 2 [person_id : 1
CountofPatents: 5]

array 3 [person_id : 2
CountofPatents: 4]

Here's how it's supposed to look like

array 0 [person_id : 1
CountofApprovedPatents: 4
CountofPatents: 5]

array 1 [person_id : 2
CountofApprovedPatents: 2
CountofPatents: 4]

Here's my code so far

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();
12
  • Doesn't your first query have 3 columns ? Where did status go? Commented Mar 11, 2019 at 12:34
  • My bad, edited my post. Commented Mar 11, 2019 at 12:38
  • If you join these 2 queries, should status = 2 apply to all rows? Commented Mar 11, 2019 at 12:41
  • No it only applies with the CountofApprovedPatents , because the other query disregards the status value. CountofPatents counts all associated with the person_id whether the status is 1 or 2. Commented Mar 11, 2019 at 12:55
  • So how would expected output look like after joining them since status = 2 doesn't apply to all? Commented Mar 11, 2019 at 12:57

2 Answers 2

2

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 :)

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the reply. I tried the above code, but I'm only getting the CountofPatents. It didn't include CountofApprovedPatents in the query.
0

You can use MySQL's IF condition in your raw selection of columns.

$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.

4 Comments

It already fetches both of CountofApprovedPatents and CountofPatents in one query. But both of them have the same count(). CountofApprovedPatents should have a lower value, i tried editing my database values to change some of the status as well.
@AndyStone My bad. It's actually sum() instead of count(). I fixed it.
Thank you thank you very much! It's working as intended. I can't thank you enough. Already accepted it as the answer. Have a good day @vivek_23
@AndyStone Welcome to SO. Glad to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.