douhezhan5348
2018-02-06 09:08
浏览 68
已采纳

mysql显示组内的unical组和sum数量

MySql I have some unical groups with 10+ lines with 2 same strings (date and ref) Mysql database:

id  name    qty     ref     weight  date
1   name1   2       x1      22      2011-01-01
2   name2   3       x1      13      2011-01-01
3   name3   4       x2      10      2021-02-02
4   name4   5       x2      15      2021-02-02

Could you please help, how I can get only unical results and summ qty and weight of the groups, like that:

ref weight  qty date
x1  35      5   2011-01-01
x2  25      9   2021-02-02

I tryed distinct, but in this case cant get sum.

图片转代码服务由CSDN问答提供 功能建议

MySql 我有一些带有10个以上相同字符串的日期组(日期和参考号) Mysql数据库:

  id name qty ref weight date 
1 name1 2 x1 22 2011-01-01 
2 name2 3 x1 13 2011-01-01 
3 name3 4 x2 10 2021-  02-02 
4 name4 5 x2 15 2021-02-02 
   
 
 

请你帮忙,我怎样才能得到唯一的结果,并总结数量和重量 组,如:

  ref weight qty date 
x1 35 5 2011-01-01 
x2 25 9 2021-02-02 
   
 
 

我尝试了不同的,但在这种情况下,不能得到总和。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanbei6427 2018-02-06 09:15
    已采纳

    You just need to use The SQL GROUP BY Statement :

    SELECT ref, sum(weight) as weight,sum(qty) as qty, date  
    FROM tablename GROUP BY date
    
    已采纳该答案
    打赏 评论
  • doulin6088 2018-02-06 09:10

    This will work for you.

    SELECT sum(weight),sum(qty) from tablename GROUP BY date
    
    打赏 评论
  • dqemld0515 2018-02-06 09:21

    Use the following query:

    SELECT ref, sim(weight) as weight, sum(qty) as qty, date
    FROM table_name
    GROUP BY CONCAT(ref, date)
    
    打赏 评论

相关推荐 更多相似问题