【问题标题】:count based on conditions mysql 5.7根据条件计算mysql 5.7
【发布时间】:2020-04-15 08:39:52
【问题描述】:

我使用的是 mysql 5.7 版,我为每个产品、数量和使用许多这样的探险都有一个表格生产

+---------+-----------------+------+--------+---------+
| Product | Type_Expedition | Pack | Amount |  Weight |
+---------+-----------------+------+--------+---------+
| Chicken | A               |    1 |      2 |       2 |
| Beef    | A               |    1 |      2 |       2 |
| Lamb    | B               |    1 |      2 |       2 |
| Beef    | B               |    2 |      2 |       4 |
| Chicken | A               |    3 |      2 |       6 |
| Lamb    | A               |    1 |      1 |       1 |
| Lamb    | A               |    1 |      1 |       1 |
+---------+-----------------+------+--------+---------+

type_expedition B 和 non-B(除 B 外的所有类型探险)如何计算重量和金额的总和?

我假设使用这种语法(对不起,我想使用 dbfiddle.uk 但这是错误的)

select product, type_expedition, pack, amount, weight, (sum(amount) where type_expedition = B), (sum(weight) where type_expedition = B) from my_table 

预期结果

+---------------------------------------------------+---+----+
|   Total amount and weight for type_expedition B   | 4 | 6  |
+---------------------------------------------------+---+----+
| Total amount and weight for type_expedition NON B | 8 | 12 |
+---------------------------------------------------+---+----+

【问题讨论】:

    标签: mysql mysql-select-db


    【解决方案1】:

    您可以对最后 2 行使用 UNION ALL:

    select t.Product, t.Type_Expedition, t.Pack, t.Amount, t.Weight
    from (
      select *, 0 sort from my_table
      union all
      select 'Total Amount and Weight for expedition B', null, null,
        sum(amount),
        sum(weight), 1
      from my_table  
      where Type_Expedition = 'B'
      union all
      select 'Total Amount and Weight for expedition not B', null, null,
        sum(amount),
        sum(weight), 2
      from my_table 
      where Type_Expedition <> 'B'
    ) t
    order by t.sort
    

    请参阅demo
    结果:

    | Product                                      | Type_Expedition | Pack | Amount | Weight |
    | -------------------------------------------- | --------------- | ---- | ------ | ------ |
    | Beef                                         | A               | 1    | 2      | 2      |
    | Chicken                                      | A               | 3    | 2      | 6      |
    | Lamb                                         | B               | 1    | 2      | 2      |
    | Lamb                                         | A               | 1    | 1      | 1      |
    | Chicken                                      | A               | 1    | 2      | 2      |
    | Beef                                         | B               | 2    | 2      | 4      |
    | Lamb                                         | A               | 1    | 1      | 1      |
    | Total Amount and Weight for expedition B     |                 |      | 4      | 6      |
    | Total Amount and Weight for expedition not B |                 |      | 8      | 12     |
    

    如果您只想要最后 2 行的总数:

    select 
      case Type_Expedition 
        when 'B' then 'Total Amount and Weight for expedition B'
        else 'Total Amount and Weight for expedition not B'
      end type,
      sum(amount),
      sum(weight)
    from my_table
    group by type
    

    请参阅demo
    结果:

    | Total Amount and Weight for expedition B     | 4           | 6           |
    | Total Amount and Weight for expedition not B | 8           | 12          |
    

    【讨论】:

    • 谢谢先生,让我申请我的真实案例,我为您提供更新,在我的真实案例中,每一列都来自另一个表(就像另一个表中的产品一样,所有coloumn)所以在我的真实案例中它包含许多连接表,我想问的是,对于我的真实案例先生,最好使用您的第一个解决方案或您的第二个解决方案?
    • 它们不一样。第二个查询仅返回总数。第一个也返回表的所有行。这取决于你想要什么。
    • 那么这两个查询的区别只是视觉上的权利?好的,谢谢你解决了,祝你今天愉快
    猜你喜欢
    • 2017-10-21
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 2018-08-07
    • 2019-07-04
    • 2021-08-31
    相关资源
    最近更新 更多