Multiple Column SUM with Join table - Laravel 5.2 - php

I have a two tables winners and check_winners.
winners:
id check_winner_id username win_number ....
1 1 xxx 1
2 2 xxx 1
3 3 yyy 1
4 4 yyy 1
check_winners:
id user_id chance_of_win....
1 1 1
2 1 1
3 2 1
4 2 1
5 2 1
6 2 1
7 2 1
8 2 1
Now i want to join two tables and want to sum chance_of_win column and win_number column group by username. I have tried an way but its not give exactly result for sum of win_number. How can i achieve this?
$winners = DB::table('winners')
->groupBy('winners.username')
->leftJoin('check_winners', 'winners.id', '=', 'check_winners.user_id')
->selectRaw('*, sum(check_winners.chance_of_win) as chance_sum, sum(winners.win_number) as win_sum')
dd($winners);
with this i get output:
array:2 [▼
0 => {#241 ▼
+"id": 1
+"check_winner_id": 1
+"username": "xxx"
+"chance_sum": "2"
+"win_sum": "2"
}
1 => {#242 ▼
+"id": 2
+"check_winner_id": 2
+"username": "yyy"
+"chance_sum": "6"
+"win_sum": "6"
}
]
but win_sum should be 2 for both array. Where i did wrong? Thanks in advance.

You should try this:
$winners = DB::table('winners')
->leftJoin('check_winners', 'winners.id', '=', 'check_winners.user_id')
->selectRaw('*, sum(check_winners.chance_of_win) as chance_sum, sum(winners.win_number) as win_sum')
->groupBy('winners.username')
dd($winners);
Updated answer
$winners = DB::table('winners')
->select('*', DB::raw("SUM(check_winners.chance_of_win) as chance_sum"), DB::raw("SUM(winners.win_number) as win_sum"))
->leftJoin('check_winners', 'winners.id', '=', 'check_winners.user_id')
->groupBy('winners.username')

Related

Count multiple columns with groupBy - laravel

I would like to count two columns in a table and group them in one query. My table:
id
is_correct
question_id
answer_id
1
0
18
67
2
0
18
67
4
1
18
68
2
0
18
67
My queries look like this:
$questions_ids = array(1, 5, 8, 10, 18, 20);
$count_total_answers = $this->whereIn('question_id', $questions_ids)
->select('question_answer_id', DB::raw('count(*) as count_answer_id'))
->addSelect('question_id')
->groupBy(['question_answer_id'])
->get()->toArray();
$count_total_questions = $this->whereIn('question_id', $questions_ids)
->select('question_id', DB::raw('count(*) as count_question_id'))
->groupBy('question_id')
->get()->toArray();
result:
result
I would like to obtain such an array:
array:2 [▼
0 => array:3 [▼
"question_answer_id" => 67
"count_answer_id" => 3
"count_question_id" => 4
]
1 => array:3 [▼
"question_answer_id" => 68
"count_answer_id" => 1
"count_question_id" => 4
]
]
Is this possible in a single query?
Greetings :)

laravel get one of each duplicate column

The structure of the table in the database is as follows:
id
body
user_id
group_id
1
test1
1
1
2
test1
2
1
3
test1
3
1
3
test2
4
2
4
test3
5
3
I want to get the first row from each group_id with Eloquent/DB Facade or MongoDB Query in Laravel with paginate:
For Example:
id
body
user_id
group_id
1
test1
1
1
3
test2
4
2
4
test3
5
3
How is this achievable?
for Mongodb use this aggregation
[
{
$sort:{
group_id: 1,
user_id:1
}
},
{
'$group': {
'_id': {
'group_id': '$group_id'
},
'a': {
'$first': '$$ROOT'
}
}
}
]
Use Eloquent's GroupBy as below, it will fetch the first record from the same group:
DB::table('tablename')
->groupBy('group_id')
->get();

Laravel order by column and relationship column

I have a query which has needs to order by additional data on relationship columns. This is my most recent attempt:
$odds = Odd::with([
'box', 'set'
])->get()->sortBy(function($query) {
return $query->box->position;
})->sortBy('created_at');
The current database tables are like so:
Odds
id. box_id. created_at
1 1 2021-04-02 17:15:04
2 2 2021-04-02 17:15:04
3 3 2021-04-02 17:15:04
4 4 2021-04-02 17:20:05
Box
id set_id position
1 1 3
2 1 1
3 1 5
4 2 2
The query above is able to sort on odds.created_at, but will not sort on the box.position. The result needs to be in date order then with in date group needs to be ordered by position, like so:
id. box_id. created_at set_id position
2 2 2021-04-02 17:15:04 1 1
1 1 2021-04-02 17:15:04 1 3
3 3 2021-04-02 17:15:04 1 5
4 4 2021-04-02 17:20:05 2 2
I have also tried these methods:
$odds = Odd::with([
'box', 'set'
])->get()->sortBy('created_at')->sortBy('box.position');
$odds = Odd::with([
'box' => function ($query) {
$query->orderBy('position');
},
'set'
])->orderBy('created_at')->get();

array_push and array_search array_splice on Laravel query builder

Are there any other eloquent query method to deal with the gross process on laravel query builder?
product table
id A_product B_product
1 4,5,8,9 3,7,10,15
2 4,7,10,11 2,3,5,13
which is better does A_product column use array or json?
----search--------------------------------------
if I want to confirm if is there 3 in A_product column on id 1
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
if(in_array("3"){
$A_result='1';
}else{
$A_result='0';
};
------insert----------------------------
if I want to add new num 20 to the A_product column
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
array_push($querys,"20");
$quearorder=array_value($querys);
DB::table('product')
->where('id', 1)
->update(['A_product' => $queryorder]);
--delete--------------------------------------
if I want to delete num 3 from the A_product column
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
$key=array_search(3,$querys);
array_splice($querys,$key,1);
DB::table('product')
->where('id', 1)
->update(['A_product' => $querys]);
Its suggestion that you should store all product into separate column , with product type and the group or for user_id
id product product_type category(or for user)
1 4 A 1
2 5 A 1
3 8 A 1
4 9 A 1
5 4 A 2
6 7 A 2
7 10 A 2
8 11 A 2
1 3 B 1
2 7 B 1
3 10 B 1
4 15 B 1
5 2 B 2
6 3 B 2
7 5 B 2
8 13 B 2
Then you can easily apply CRUD operation

array values with where condition in laravel query

I have an array called assetIDs as below
$assetIDs = Collection {#505 ▼
#items: array:2 [▼
0 => 4
1 => 7
]
}
and I have the data in the table as below shown
and I'm doing query on the above table using this
$supplier_id = SupplierAsset::whereIn('asset_id',$asset_ids)->pluck('supplier_id');
and result for the above query is below
Collection {#510 ▼
#items: array:3 [▼
0 => 1
1 => 1
2 => 2
]
}
here whereIn is returning all the possible rows which satisfies the condition. Actually I need to get the result as like which supplier_id has both the values of assetIDs array.In my table supplier_id=1 has the both values 4 and 7 Just like below collection.
Collection {#510 ▼
#items: array:3 [▼
0 => 1
1 => 1
]
}
Can anybody suggest me the solution for this please?
You can try:
$supplier_id = SupplierAsset::whereIn('asset_id',$asset_ids)
->groupBy('supplier_id')
->havingRaw('count(distinct asset_id) = ' . count($assetIDs))
->pluck('supplier_id');
Here is the mysql you should do :
1- get all the id who have more than 1 supplier_id :
SELECT supplier_id, Count(distinct asset_id) as dist_asst
FROM Table
GROUP BY supplier_id
HAVING dist_asst > 1
2- then doing a join :
SELECT t1.supplier_id
FROM Table t1
INNER JOIN (SELECT supplier_id, Count(distinct asset_id) as dist_asst
FROM Table
GROUP BY supplier_id
HAVING dist_asst > 1) t2 on t2.supplier_id = t1.supplier_id

Categories