如何在 postgresql 中加入两个以上的 table?

how to join more then two table in postgresql?

我正在 运行 关注查询

select 
    to_char(sale_order.date_order ,'DD-MM-YYYY') , sum(sale_order.amount_total) as amount

from 
    public.sale_order
where
   sale_order.order_year = '2015' and
   sale_order.order_month = 'April'
group by 
   to_char(sale_order.date_order ,'DD-MM-YYYY') order by to_char(sale_order.date_order ,'DD-MM-YYYY') asc

它给出了正确的输出

   to_char..     amount

"14-04-2015";   1298.00
"15-04-2015";   4294.00
"16-04-2015";   1398.00
"17-04-2015";   1927.00
"18-04-2015";   3094.00
"19-04-2015";   6988.00
"20-04-2015";   6641.00
"21-04-2015";   3045.00

但我正在尝试输入一个有多个 table 连接的条件,然后它给出不同的金额值

select 
    to_char(sale_order.date_order ,'DD-MM-YYYY') , sum(sale_order.amount_total) as amount

from 
    public.sale_order ,
    public.sale_order_line , 
    public.product_product ,
    public.product_template ,
    public.product_category

where
    sale_order_line.product_id = product_product.id AND
  product_product.product_tmpl_id = product_template.id AND
  product_template.categ_id = product_category.id AND
  sale_order.id = sale_order_line.order_id AND
  sale_order_line.product_id = product_product.id AND 
  product_product.product_tmpl_id = product_template.id AND 
  product_template.categ_id = product_category.id AND
  product_category.name = 'Starchi' and
    sale_order.order_year = '2015' and
  sale_order.order_month = 'April'
group by to_char(sale_order.date_order ,'DD-MM-YYYY') order by to_char(sale_order.date_order ,'DD-MM-YYYY') asc

然后它给出不同的输出

  to_char         amount
"14-04-2015";    1298.00
"15-04-2015";    4294.00
"16-04-2015";    1398.00
"17-04-2015";    2805.00     //wrong output
"18-04-2015";    6188.00    //wrong output
"19-04-2015";    13976.00  //wrong output
"20-04-2015";    19229.00  //wrong output
"21-04-2015";    3045.00

具体问题是什么,谁能告诉我?

如何解决?

您在 WHERE 子句中犯了一个错误,正如戈登·利诺夫 (Gordon Linoff) 已经评论过的那样,导致检索到重复的行,然后将其加起来得出错误的总计。按照 wildplasser 在他的评论中的建议,使用 JOIN 语法更加清晰且不易出错。您的查询将如下所示:

SELECT to_char(o.date_order, 'DD-MM-YYYY') AS "date", sum(o.amount_total) AS amount
FROM public.sale_order o
JOIN public.sale_order_line l ON l.order_id = o.id
JOIN public.product_product pp ON pp.id = l.product_id
JOIN public.product_template pt ON pt.id = pp.product_tmpl_id
JOIN public.product_category pc ON pc.id = pt.categ_id
WHERE pc.name = 'Starchi'
  AND o.order_year = '2015'
  AND o.order_month = 'April'
GROUP BY o.date_order
ORDER BY o.date_order ASC

[使用@Patrick 清理过的代码]最可能的原因是订单和 order_lines 之间存在 1:N 关系,导致包含多个 order_lines 的订单在 sum() 中不止一次。

SELECT to_char(o.date_order, 'DD-MM-YYYY') AS date_order
        , sum(o.amount_total) AS amount
FROM public.sale_order o
WHERE EXISTS ( SELECT *
        -- These tables probably have an N:1 relation
        -- to public.sale_order
        -- We don't have to sum them; just check existence.
        FROM public.sale_order_line l
        JOIN public.product_product pp ON pp.id = l.product_id
        JOIN public.product_template pt ON pt.id = pp.product_tmpl_id
        JOIN public.product_category pc ON pc.id = pt.categ_id
        WHERE pc.name = 'Starchi'
        AND l.order_id = o.id
        )
  AND o.order_year = '2015'
  AND o.order_month = 'April'
GROUP BY o.date_order
ORDER BY o.date_order ASC
        ;