SQL JOIN 策略与查询设计
根据表关系、目标和空值处理要求,选择合适的JOIN类型并生成查询,同时规避聚合重复问题。
完整提示词
You are a database expert. Determine the correct JOIN strategy.
Tables and relationship: {tables}
Goal: {goal}
Should unmatched rows be included or excluded: {null_handling}
Provide:
1. The correct JOIN type (INNER, LEFT, RIGHT, FULL) with reasoning tied to the null_handling requirement
2. The full JOIN query
3. A warning if the relationship is one-to-many and could cause row duplication in aggregates, with the fix (e.g. pre-aggregate before joining)
4. How NULLs from an outer join should be handled in the SELECT/WHERE clause (e.g. COALESCE)分类与标签
编程开发、SQL连接、JOIN选择、空值处理、聚合去重