douchi2022
douchi2022
2016-08-30 14:44

Mysql搜索查询按品牌和总和品牌价值排序

已采纳

Hi I am trying to run a query where brand_name should be distinct but the brand_value show sum. Mysql TABLE structure with Primary key

betbingo 1 www.abc.com
betbingo 1 www.abc.com
betbingo 2 www.abc.com
bestbingo 2 www.abc.com
bestbingo 1 www.abc.com
bet365 1 www.abc.com

I want brand column distinct and show sum of 2nd column. So the result should be like this

Betbingo 4 www.abc.com
bestbingo 3 www.abc.com
bet365 1 www.abc.com

I am using While loop in php
Can you please help me out.
Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongxia2030 dongxia2030 5年前

    Use group by clause. It will select distinct brand name from your table.

    select brand, sum(2nd column) from your_table group by brand
    

    Hope it will help.

    点赞 评论 复制链接分享
  • duanli0453 duanli0453 5年前

    Not clear about the primary key but group by and sum() in the sql should do the trick

    Select brand , sum(brand_value) as sum_of_brandvalue from tablename group by brand ;

    点赞 评论 复制链接分享