【问题标题】:Laravel Eloquent Query causes Application to continuously loadLaravel Eloquent Query 导致应用程序不断加载
【发布时间】:2016-10-31 10:34:52
【问题描述】:

我为我的应用程序中的一个表编写了以下雄辩的查询,现在应用程序不会停止加载。我需要的是了解它有什么问题以及如何解决它

    $customer_purchased_1_time=DB::table("data as t1")->select('vin','last_service')
              ->where('type',1)->distinct('vin')
              ->whereRaw("(select count(*) from data where t1.vin=data.vin and data.type=1)=1")
              ->count();

这是我的控制器方法中唯一的代码以及返回语句。 x 的递增是为了调试

【问题讨论】:

  • 也许发布其余代码,因为它看起来像上面的代码在循环中 - “连续加载”中的第一名嫌疑人,也就是无限递归 :-)
  • 我同意@theruss。另外,在查询前后增加$x 有点奇怪。
  • 哈!没看到,不错!
  • 你每件事都做了两次或三次。您可以完全删除 ->whereRaw() 因为它是多余的。 $x 临时变量也一样

标签: php mysql sql laravel eloquent


【解决方案1】:

我已经解决了。这段代码对我来说很好用

$customer_purchased_1_time=count(DB::table("data as t1")
              ->select('vin','last_service',DB::raw("count(*) as count"))
              ->where('type',1)
              ->distinct('vin')
              ->having("count","=",1)
              ->get());

【讨论】:

    猜你喜欢
    • 2021-04-12
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 2018-10-10
    相关资源
    最近更新 更多