mysql - 使用 Laravel 构建器同时为子查询、过滤器、计数创建查询

标签 mysql laravel postgresql

我有一个这样的查询。我尝试将其转换为查询生成器样式。但由于它太复杂,我遇到了麻烦。

我喜欢使用 whereIn 。但是,如果我使用原始数据库查询,我必须自己编码(获取集合,放置逗号等),以便我正在寻找一种使其对 Laravel 查询生成器友好的方法。

SELECT
    total_amount_until_now/total_orders_until_now as avg_order_value_now,
    total_amount_until_a_month_ago/total_orders_until_a_month_ago as avg_order_value_until_a_month_ago

FROM(
SELECT
  COUNT(DISTINCT a.order_id)
    FILTER (WHERE CURRENT_DATE >= b.order_creation_date AND b.seller_id IN (1, 3))                     AS total_orders_until_now,
  SUM(invoice_amount)
    FILTER (WHERE CURRENT_DATE >= b.order_creation_date AND b.seller_id IN (1, 3))                     AS total_amount_until_now,
  COUNT(DISTINCT a.order_id)
    FILTER (WHERE CURRENT_DATE - INTERVAL '1 month' > b.order_creation_date AND b.seller_id IN (1, 3)) AS total_orders_until_a_month_ago,
  SUM(invoice_amount)
    FILTER (WHERE CURRENT_DATE - INTERVAL '1 month' > b.order_creation_date AND b.seller_id IN (1, 3)) AS total_amount_until_a_month_ago
FROM
  order_items a
  INNER JOIN orders b ON a.order_id = b.order_id)  xyz"

这是我的尝试,但没有成功

DB::select()
        ->selectSub(`total_amount_until_now`, `avg_order_value_now`)
        ->selectSub(`/`, `avg_order_value_now`)
        ->selectSub(`total_orders_until_now`, `avg_order_value_now`)
        ->selectSub(`total_amount_until_a_month_ago`, `avg_order_value_until_a_month_ago`)
        ->selectSub(`/`, `avg_order_value_until_a_month_ago`)
        ->selectSub(`total_orders_until_a_month_ago`, `avg_order_value_until_a_month_ago`)
        ->from(`SELECT COUNT(DISTINCT a.order_id) FILTER ( WHERE CURRENT_DATE >= b.order_creation_date AND b.seller_id IN (1, 3) ) AS total_orders_until_now, SUM(invoice_amount) FILTER ( WHERE CURRENT_DATE >= b.order_creation_date AND b.seller_id IN (1, 3) ) AS total_amount_until_now, COUNT(DISTINCT a.order_id) FILTER ( WHERE CURRENT_DATE - INTERVAL 1 month > b.order_creation_date AND b.seller_id IN (1, 3) ) AS total_orders_until_a_month_ago, SUM(invoice_amount) FILTER ( WHERE CURRENT_DATE - INTERVAL 1 month > b.order_creation_date AND b.seller_id IN (1, 3) ) AS total_amount_until_a_month_ago FROM order_items a INNER JOIN orders b ON a.order_id = b.order_id as xyz`)
        ->get();

最佳答案

$from = DB::table('order_items AS a')
    ->selectRaw('COUNT(DISTINCT a.order_id) FILTER (WHERE CURRENT_DATE >= b.order_creation_date AND b.seller_id IN (?, ?)) AS total_orders_until_now', [1, 3])
    ->selectRaw('SUM(invoice_amount) FILTER (WHERE CURRENT_DATE >= b.order_creation_date AND b.seller_id IN (?, ?)) AS total_amount_until_now', [1, 3])
    ->selectRaw("COUNT(DISTINCT a.order_id) FILTER (WHERE CURRENT_DATE - INTERVAL '1 month' > b.order_creation_date AND b.seller_id IN (?, ?)) AS total_orders_until_a_month_ago", [1, 3])
    ->selectRaw("SUM(invoice_amount) FILTER (WHERE CURRENT_DATE - INTERVAL '1 month' > b.order_creation_date AND b.seller_id IN (?, ?)) AS total_amount_until_a_month_ago", [1, 3])
    ->join('orders AS b', 'a.order_id', '=', 'b.order_id');
DB::query()
    ->selectRaw('total_amount_until_now/total_orders_until_now as avg_order_value_now')
    ->selectRaw('total_amount_until_a_month_ago/total_orders_until_a_month_ago as avg_order_value_until_a_month_ago')
    ->fromSub($from, 'xyz')
    ->get();

您可能还可以使用绑定(bind) (?) 来表示 '1 个月',但我不完全确定。你必须尝试一下。

关于mysql - 使用 Laravel 构建器同时为子查询、过滤器、计数创建查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49659174/

相关文章:

php - 如何在 Laravel 5.4 中多次使用变量模型

postgresql - 如何将 Postgres 驱动程序添加到 Open Liberty?

php - 带有参数的joomla mysql查询

mysql - 如何更改 Django 默认测试数据库?

php - Laravel 4,困惑的 Eloquent 连接查询

php - sync or updateExistingPivot with Laravel——如何根据第三个标准进行填充

postgresql - 将 azure 文件存储与 kubernetes 和 azure 容器服务 (aks) 一起使用 - 权限被拒绝

postgresql - 尝试实现 "geo"postgres 扩展类型时出错

mysql - 如何在mysql中选择前3名高分

带递归的 MySQL