单个Laravel中的多条件计数选择查询

发布于 2025-01-22 16:31:31 字数 323 浏览 0 评论 0原文

我正在尝试根据不同的条件来计数票证。我在同一模型上使用了四个不同的查询。我可以仅使用一个查询执行所有计算吗?

$openTickets = Ticket::where('status',1)->count();
$pending = Ticket::where('status',2)->count();
$unAssigned = Ticket::where('agent_id',null)->count();
$unResolved = Ticket::whereNotIn('status',[3,4])->count();

I am trying to count tickets based on different where conditions . I am using four different queries on the same model. Can I execute all of the calculations using just one query?

$openTickets = Ticket::where('status',1)->count();
$pending = Ticket::where('status',2)->count();
$unAssigned = Ticket::where('agent_id',null)->count();
$unResolved = Ticket::whereNotIn('status',[3,4])->count();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若相惜即相离 2025-01-29 16:31:31
Ticket::selectRaw('COUNT(CASE WHEN status = 1 THEN 1 END) AS open_tickets')
      ->selectRaw('COUNT(CASE WHEN status = 2 THEN 1 END) AS pending_tickets')
      ->selectRaw('COUNT(CASE WHEN agent_id IS NULL THEN 1 END) AS unassigned_tickets')
      ->selectRaw('COUNT(CASE WHEN status NOT IN (3,4) THEN 1 END) AS unresolved_tickets')
      ->first();

您当然可以通过此查询解决多个查询。我们可以使用条件案例和计数。

Ticket::selectRaw('COUNT(CASE WHEN status = 1 THEN 1 END) AS open_tickets')
      ->selectRaw('COUNT(CASE WHEN status = 2 THEN 1 END) AS pending_tickets')
      ->selectRaw('COUNT(CASE WHEN agent_id IS NULL THEN 1 END) AS unassigned_tickets')
      ->selectRaw('COUNT(CASE WHEN status NOT IN (3,4) THEN 1 END) AS unresolved_tickets')
      ->first();

You can of course resolve the multiple queries with this query. We can use conditional cases and count.

我家小可爱 2025-01-29 16:31:31

您可以总结有条件的条件,但在查询中需要许多原始零件:

$result = DB::table('tickets')->whereIn('status', [1,2])->orWhereNull('agent_id')->orWhereNotIn('status', [3,4]) // This is to filter out the things we don't care about
->select([
   DB::raw('SUM(IF(status = 1, 1,0)) as countOpen'),
   DB::raw('SUM(IF(status = 2, 1,0)) as countPending'),
   DB::raw('SUM(IF(agent_id IS NULL, 1,0)) as countUnassigned'),
   DB::raw('SUM(IF(status NOT IN (3,4), 1,0)) as countUnresolved'),
])->first()
$openTickets = $result->countOpen;
$pending = $result->countPending;
$unAssigned = $result->countUnassigned;
$unResolved = $result->countUnresolved;

You can sum up conditionals but will need lots of raw parts in your query:

$result = DB::table('tickets')->whereIn('status', [1,2])->orWhereNull('agent_id')->orWhereNotIn('status', [3,4]) // This is to filter out the things we don't care about
->select([
   DB::raw('SUM(IF(status = 1, 1,0)) as countOpen'),
   DB::raw('SUM(IF(status = 2, 1,0)) as countPending'),
   DB::raw('SUM(IF(agent_id IS NULL, 1,0)) as countUnassigned'),
   DB::raw('SUM(IF(status NOT IN (3,4), 1,0)) as countUnresolved'),
])->first()
$openTickets = $result->countOpen;
$pending = $result->countPending;
$unAssigned = $result->countUnassigned;
$unResolved = $result->countUnresolved;
温柔一刀 2025-01-29 16:31:31

count()在语义上适合此任务,而sum()可以更方便地消耗有条件的表达式,并取决于false/true结果的效果为0或1每个评估。使用selectraw()一次或四次构建Select子句,然后first()填充具有所需条件计数的对象。

代码:( phpize demo

var_export(
    DB::table('tickets')
    ->selectRaw("SUM(status = 1) open")
    ->selectRaw("SUM(status = 2) pending")
    ->selectRaw("SUM(agent_id IS NULL) unassigned")
    ->selectRaw("SUM(status NOT IN (3,4)) unresolved")
    ->first()
);

输出:

(object) array(
   'open' => '2',
   'pending' => '3',
   'unassigned' => '2',
   'unresolved' => '6',
)

While COUNT() is semantically appropriate for this task, SUM() can more conveniently consume a conditional expression with the effect of summing 0 or 1 depending on the false/true result of each evaluation. Use selectRaw() one or four times to build the SELECT clause, then first() to populate an object with the desired conditional counts.

Code: (PHPize Demo)

var_export(
    DB::table('tickets')
    ->selectRaw("SUM(status = 1) open")
    ->selectRaw("SUM(status = 2) pending")
    ->selectRaw("SUM(agent_id IS NULL) unassigned")
    ->selectRaw("SUM(status NOT IN (3,4)) unresolved")
    ->first()
);

Output:

(object) array(
   'open' => '2',
   'pending' => '3',
   'unassigned' => '2',
   'unresolved' => '6',
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文