Laravel DB查询的正确格式

时间:2019-02-19 15:27:53

标签: laravel laravel-5

由于某种原因,当我尝试将查询从原始SQL移至Laravel DB时,我一定会丢失一些东西。

此刻,使用此SQL查询:

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;

我获得了三(3)个结果,并且正确设置了计数返回值。但是,当我尝试使用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();

我什么也没得到。我知道原始SQL查询正确无误,因为我已经手动对其进行了检查,因此我知道Laravel中的$results应该会返回一些信息。

我将不胜感激。谢谢

//以查询日期更新//

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
  ]
]

///从GROUP BY删除ID后更新

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_items where agentBillingBatch = 224 and shipmentID is not null and deleted_at is null group by shipmentID having count > 1)

0 个答案:

没有答案