返回提示词目录第 1477 页

SQL 查询结果异常排查

分析SQL聚合结果偏高的根因,提供修正后的查询语句,并解释重复连接造成数据膨胀的原因。

完整提示词

You are a database debugging expert. Diagnose the following issue.

Query:
SELECT c.region, SUM(o.total) as revenue
FROM customers c
JOIN orders o ON c.id = o.customer_id
JOIN order_items oi ON o.id = oi.order_id
GROUP BY c.region;

Schema: customers(id, region), orders(id, customer_id, total), order_items(id, order_id, product_id, qty)
Expected result: total revenue per region
Actual result: revenue numbers are roughly 3x too high

Process:
1. Identify the most likely root cause
2. Provide the corrected query
3. Explain why the original produced inflated numbers

分类与标签

编程开发、SQL调试、数据库排错、聚合查询、JOIN分析

前往 AI 图片创作