For some reason, I must be missing something when I am trying to move my query from raw SQL to Laravel DB.
At the moment, with this SQL query:
SELECT id, COUNT(id) AS count FROM agent_billings_items WHERE agentBillingBatch = 224 AND shipmentID IS NOT NULL GROUP BY shipmentID HAVING COUNT(id) > 1;
I get three (3) results back with the count return set correctly. However, when I try and write this out using Laravel DB:
$results = DB::table('agent_billings_items')
->select('id', DB::raw('count(*) as count'))
->where('agentBillingBatch', $this->id)
->whereNotNull('shipmentID')
->whereNull('deleted_at')
->groupBy('shipmentID')
->havingRaw('count > 1')
->get();
I get nothing returned. I know the raw SQL query is correct as I have manually checked it, so I know the $results
in Laravel should come back with something.
I'd appreciate any help. Thanks
//UPDATE WITH DD OF QUERY//
array:1 [▼
0 => array:3 [▼
"query" => "select `id`, count(*) as count from `agent_billings_items` where `agentBillingBatch` = ? and `shipmentID` is not null and `deleted_at` is null group by `shipmentID`, `id` having count > 1 ◀"
"bindings" => array:1 [▼
0 => 224
]
"time" => 41.85
]
]
//UPDATE AFTER REMOVING id FROM GROUP BY
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'truckin.agent_billings_items.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select
id, count(*) as count from
agent_billings_itemswhere
agentBillingBatch= 224 and
shipmentIDis not null and
deleted_atis null group by
shipmentIDhaving count > 1)