mySQL Group按查询无效

时间:2011-07-19 16:34:59

标签: mysql

我想加入下表。我有疑问:

SELECT MAX(s.Date) AS last_ship_date,
    s.Form AS summary_form, 
    SUM(s.Total) AS qty_ship,
    SUM(f.Quantity) AS qty_req
    FROM Slip AS s
    LEFT OUTER JOIN Forms AS f ON s.Form=f.Form
    AND s.DocketNumber=f.DocketNumber
    WHERE s.DocketNumber=20491
    GROUP BY s.Form

此查询为英语提供 qty_req 为210000,而我想要的是105000.我做错了什么?

编辑:我希望在两个表中都有倍数。这就是我使用SUM的原因。我希望按表单汇总。

注意:如果我只执行以下操作,那么我就会获得105000英语,就像我想要的那样。

SELECT SUM(f.Quantity) AS qty_req FROM Forms AS f
WHERE f.DocketNumber=20491 
GROUP BY f.Form 

表单

| Form    | Quantity | DocketNumber | Date
| English | 105000| 20491
| French  | 105000 | 20491

表单

| Form    | DocketNumber | Total | Date
| English | 20491        | 7000
| English | 20491        | 1000
| English | 20491        | 1000
| French  | 20491        | 7000

1 个答案:

答案 0 :(得分:0)

LEFT OUTER JOIN表格AS f ON s.Form = f.Form     AND s.DocketNumber = f.DocketNumber

如果我正确读取了您的表值,则此连接会导致每次滑动多次匹配,这会导致它多次相加相同的值。