gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:47:54 25 4
gpt4 key购买 nike

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

我喜欢使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com