【问题标题】:Laravel - Select Count with no field data repeatedLaravel - 选择不重复字段数据的计数
【发布时间】:2020-02-01 16:39:27
【问题描述】:

我正在使用 Laravel-5.8 Eloquent 运行查询。我想获得特定客户在所有旅行中使用的卡车总数,而不用两次计算卡车。我使用了下面的查询,但重复使用了不止一次的卡车:

  $truckcount = Trip::where('client_id', $userClientId)
   ->select(\DB::raw("COUNT(truck_no) as count"))
   ->distinct()   
   ->get();

如何编写正确的 Eloquent 查询来实现这一点?

【问题讨论】:

标签: laravel eloquent


【解决方案1】:

试试这个:

 $truckcount = Trip::where('client_id', $userClientId)
   ->select(\DB::raw("COUNT(DISTINCT truck_no) as count"))
   ->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2021-04-05
    • 2019-04-24
    • 2021-12-25
    • 2015-08-27
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多