如何在同一查询中使用group by和left and inner join?

时间:2017-01-11 10:06:08

标签: mysql join

我正在使用内部联接和左联接的查询来从3个表中获取数据。我也想使用matname来使用group。因为同一个matname会有多个输出。我想得到它们的总和。

表tbl_sales

+------------+--------------+---------------+----------+--------+---------+
| company    | customer     | date          | material | matgrp | qty_ach |
+------------+--------------+---------------+----------+--------+---------+
| 2100       | 30000002     | 2017-01-10    |  2000012 | FG0001 |    2    |
| 2200       | 30000002     | 2017-01-10    |  2000012 | FG0001 |    4    |
| 2100       | 30000003     | 2017-01-10    |  2000011 | FG0001 |    5    |
| 2100       | 30000002     | 2017-01-11    |  2000024 | FG0001 |    4    |
| 2300       | 30000004     | 2017-01-11    |  2000010 | FG0001 |    1    |
+------------+--------------+---------------+----------+--------+---------+

表tbl_mas_material

+---------+--------------+--------+
| matnum  | matname      | matgrp | 
+---------+--------------+--------+
| 2000011 | name1        | FG0001 | 
| 2000012 | name2        | FG0001 |  
| 2000013 | name3        | FG0001 |    
| 2000014 | name4        | FG0001 |   
| 2000015 | name5        | FG0001 |    
+---------+--------------+--------+

表tbl_mas_customer

+---------+----------+--------+
| customer| rep      | namez  | 
+---------+----------+--------+
| 2000011 | 501      | ddd    | 
| 2000012 | 502      | bbb    |  
| 2000013 | 501      | zzz    |    
| 2000014 | 501      | qqq    |   
| 2000015 | 503      | ttt    |    
+---------+----------+--------+

我想获得特定材料组(matgrp)的数量(qty_ach)及其特定代表已售出的名称(matname)。

这是我使用的查询。这给了我正确的输出。

SELECT tbl_mas_material.matname, tbl_sales.qty_ach 
FROM salesforce.tbl_sales 
inner join salesforce.tbl_mas_customer on salesforce.tbl_mas_customer.customer = salesforce.tbl_sales.customer 
left join salesforce.tbl_mas_material on salesforce.tbl_mas_material.matnum = salesforce.tbl_sales.material 
where salesforce.tbl_sales.matgrp = 'FG0001' 
and DATE_FORMAT(date, '%Y-%m') = '2017-01' 
and salesforce.tbl_mas_customer.rep = '501'

但是我希望使用 matname 对他们进行分组,因为我想使用相同名称的材料获取qty_ach的总和。但是如果我最后用matname添加group,它就不会给我正确的输出和qty_ach的总和

没有分组

    {"feed":[
    {"matname":"3.00-10 4PR SECURA NEO TL CEAT","qty_ach":"5"},
    {"matname":"3.50-10 04PR SECURA NEO CEAT","qty_ach":"2"},
    {"matname":"3.00-18 6PR SECURA ZOOM TL","qty_ach":"1"},
    {"matname":"2.75-17 04PR SECURA ZOOM CEAT (F)","qty_ach":"17"},
    {"matname":"3.00-18 06PR SECURA SPORT CEAT (R)","qty_ach":"3"},
{"matname":"100\/90-18 GRIPP TL CEAT","qty_ach":"5"},
    {"matname":"100\/90-17 GRIPP TL CEAT","qty_ach":"6"},
    {"matname":"2.75-18 04PR SECURA ZOOM (F)","qty_ach":"-11"},
    {"matname":"3.00-17 06PR SECURA ZOOM CEAT (R)","qty_ach":"-11"},       
{"matname":"3.00-17 06PR SECURA ZOOM CEAT (R)","qty_ach":"11"},
    {"matname":"3.00-17 04PR GRIPP","qty_ach":"33"},
    {"matname":null,"qty_ach":"22"},
    {"matname":"3.00-17 06PR SECURA ZOOM CEAT (R)","qty_ach":"11"},
    {"matname":"2.50-17 04PR SECURA SPORT CEAT  (R)","qty_ach":"4"},
    {"matname":"2.50-17 04PR SECURA ZOOM FX (F)","qty_ach":"3"},
    {"matname":"2.75-17 04PR SECURA ZOOM CEAT (F)","qty_ach":"2"},
    {"matname":"3.00-17 06PR SECURA ZOOM CEAT (R)","qty_ach":"22"},
    {"matname":"3.00-17 06PR SECURA ZOOM CEAT (R)","qty_ach":"6"}
{"matname":"100\/90-17 SECURA ZOOM CEAT TL","qty_ach":"22"},
    {"matname":"100\/90-17 SECURA ZOOM CEAT TL","qty_ach":"5"},
    {"matname":"100\/90-17 SECURA ZOOM CEAT TL","qty_ach":"6"},
    {"matname":"100\/90-18 6PR SECURA ZOOM TL","qty_ach":"12"},
    {"matname":"3.00-18 6PR SECURA ZOOM TL","qty_ach":"4"},
    {"matname":"2.75-14 06PR SECURA 275","qty_ach":"2"},
    {"matname":"2.75-14 06PR SECURA 275","qty_ach":"6"},
    {"matname":"2.75-17 04PR SECURA ZOOM TL","qty_ach":"10"},
    {"matname":"2.75-17 04PR SECURA ZOOM TL","qty_ach":"3"},
    {"matname":"2.75-17 04PR SECURA ZOOM TL","qty_ach":"27"},
    {"matname":"2.75-17 04PR SECURA ZOOM TL","qty_ach":"27"},
    {"matname":"3.00-17 04PR GRIPP","qty_ach":"33"},
    {"matname":"3.00-17 04PR GRIPP","qty_ach":"11"},
    {"matname":"100\/90-18 GRIPP TL CEAT","qty_ach":"5"},
    {"matname":"120\/80-17 SECURA PULSE","qty_ach":"6"},
    {"matname":"100\/90-17 GRIPP TL CEAT","qty_ach":"5"},
{"matname":null,"qty_ach":"22"},
    {"matname":null,"qty_ach":"38"},
    {"matname":null,"qty_ach":"11"},
    {"matname":null,"qty_ach":"11"},
    {"matname":null,"qty_ach":"22"},
    {"matname":null,"qty_ach":"5"},
    {"matname":null,"qty_ach":"-5"},
    {"matname":null,"qty_ach":"7"}]}

with group by tbl_mas_material.matname

{"feed":[{"matname":null,"qty_ach":"22"},
{"matname":"100\/90-17 GRIPP TL CEAT","qty_ach":"6"},
{"matname":"100\/90-17 SECURA ZOOM CEAT TL","qty_ach":"22"},
{"matname":"100\/90-18 6PR SECURA ZOOM TL","qty_ach":"12"},
{"matname":"100\/90-18 GRIPP TL CEAT","qty_ach":"5"},
{"matname":"120\/80-17 SECURA PULSE","qty_ach":"6"},
{"matname":"2.50-17 04PR SECURA SPORT CEAT  (R)","qty_ach":"4"},{"matname":"2.50-17 04PR SECURA ZOOM FX (F)","qty_ach":"3"},
{"matname":"2.75-14 06PR SECURA 275","qty_ach":"2"},
{"matname":"2.75-17 04PR SECURA ZOOM CEAT (F)","qty_ach":"17"},
{"matname":"2.75-17 04PR SECURA ZOOM TL","qty_ach":"10"},
{"matname":"2.75-18 04PR SECURA ZOOM (F)","qty_ach":"-11"},
{"matname":"3.00-10 4PR SECURA NEO TL CEAT","qty_ach":"5"},
{"matname":"3.00-17 04PR GRIPP","qty_ach":"33"},
{"matname":"3.00-17 06PR SECURA ZOOM CEAT (R)","qty_ach":"-11"},{"matname":"3.00-18 06PR SECURA SPORT CEAT (R)","qty_ach":"3"},
{"matname":"3.00-18 6PR SECURA ZOOM TL","qty_ach":"1"},
{"matname":"3.50-10 04PR SECURA NEO CEAT","qty_ach":"2"}]}

问题在于输出不是我想要的。请参阅matname:null。它只给了我22后使用组。但我想要它们的总和(matname为null):22 + 38 + 11 + 11 + 22 + 5-5 + 7 = 111

1 个答案:

答案 0 :(得分:1)

嗯,我在这里猜测,因为你没有分享确切的预期输出。但是,根据您编写的事实,您希望“获取qty_ach的总和”并且在当前输出qty_ach中没有总结,您在查询中没有使用sum() group by函数。没有它,MySQL无法告诉你想要总结这个字段。

在这种情况下,查询看起来像下面的行:

SELECT tbl_mas_material.matname, sum(tbl_sales.qty_ach) as  qty_ach_sum
FROM salesforce.tbl_sales 
inner join salesforce.tbl_mas_customer on salesforce.tbl_mas_customer.customer = salesforce.tbl_sales.customer 
left join salesforce.tbl_mas_material on salesforce.tbl_mas_material.matnum = salesforce.tbl_sales.material 
where salesforce.tbl_sales.matgrp = 'FG0001' 
and DATE_FORMAT(date, '%Y-%m') = '2017-01' 
and salesforce.tbl_mas_customer.rep = '501'
group by tbl_mas_material.matname