【问题标题】:Laravel Eloquent Multiple Where with countLaravel Eloquent Multiple Where with count
【发布时间】:2021-09-08 03:52:44
【问题描述】:

以下是我需要在 laravel eloquent 上执行的 SQL 查询。 SQL 返回预期的输出。

SELECT
  orders.id,
  orders.`status`,
  order_type,
  COUNT(order_type) as count
FROM
  orders
WHERE
  orders.`status` = 0 && order_type = 1
ORDER BY
  orders.id DESC

我在 laravel 上试过的如下

        $receved = Order::select('status', 'order_type')->where('status',0);
        $relase = $receved->where('order_type',  1)->get();
        $bulk = $receved->where('order_type', 2)->get();
        $distiribute = $receved->where('order_type', 3)->get();

        return response()->json([

            'success' => true,
            'message' => 'Statement Updated',
            'orderStatment' =>  [
                'relaseCount' => count($relase),
                'bulkCount' =>  count($bulk),
                'distiributeCount' => count($distiribute)
            ],
        ], 200);

我寻求建议/建议以正确的方式进行操作

我在 laravel 上得到的输出是

            'orderStatment' =>  [
                'relaseCount' => 14,
                'bulkCount' =>  0,
                'distiributeCount' => 0
            ],

期望和SQL产生的输出是

                    'orderStatment' =>  [
                        'relaseCount' => 14,
                        'bulkCount' =>  5,
                        'distiributeCount' => 4
                    ],

表上有 8 种不同类型的 status 和 3 种不同类型的 order_type 我想获取每个状态的每个 order_type 计数

【问题讨论】:

  • 你得到了什么与你期望什么?
  • @BrianThompson 我更新了问题并提供了更多信息

标签: php laravel eloquent laravel-8


【解决方案1】:

您可能会在一个查询中完成所有操作,然后将数据取回。

$receved = Order::select('status', 'order_type', DB::raw('COUNT(id) as order_count'))->where('status',0)
              ->groupBy('order_type')
              ->get();

这将在一个查询中为您提供所有订单类型及其计数的集合。之后,您可以将数据取回。

$bulk = $relase = $distiribute = 0;
foreach($receved as $rec) {
      if($rec->order_type == 1) $relase = $rec->order_count;
      elseif($rec->order_type == 2) $bulk = $rec->order_count;
      elseif($rec->order_type == 3) $distiribute = $rec->order_count;
}

【讨论】:

  • 返回错误SQLSTATE[42000]: Syntax error or access violation: 1055
  • 你能提供完整的错误信息吗?我可能打错了什么,但我看不到它
  • message: "SQLSTATE[42000]: Syntax error or access violation: 1055 'vgc.orders.status' isn't in GROUP BY (SQL: select status, order_type, COUNT(id) as order_count from orders` 其中status = 0 group by order_type)"
  • 啊哈,那该死的 GROUP BY 消息。在您的查询前添加DB::statement("set @@sql_mode='ONLY_FULL_GROUP_BY'");。或者在config/database.php中,将strict => false改成mysql
【解决方案2】:

您面临的问题是由于以下所有语句都在操作同一个查询构建器对象:

$receved = Order::select('status', 'order_type')->where('status',0);
$relase = $receved->where('order_type',  1)->get();
$bulk = $receved->where('order_type', 2)->get();
$distiribute = $receved->where('order_type', 3)->get();

所以实际创建的查询将是这样的:

全部以:select status, order_type from orders where status = 0 and

开头
  1. order_type = 1;
  2. order_type = 1 and order_type = 2;
  3. order_type = 1 and order_type = 2 and order_type = 3;

这就是最后两个查询返回 0 的原因。一旦您看到结果查询,这是预期的。

您可以通过记录查询来验证这一点(请参阅this answer for detailsdocs here)。

$receved 实际上每次都附加了where 子句。因此,您不只是从原始声明开始,而是每次调用 where构建它。

【讨论】:

    猜你喜欢
    • 2020-07-09
    • 2021-07-12
    • 2016-08-08
    • 1970-01-01
    • 2011-01-21
    • 2021-02-15
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多