跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • [请教] 同一张表根据指定 key 分组,并执行双重统计
未分類
15 5 月 2020

[请教] 同一张表根据指定 key 分组,并执行双重统计

[请教] 同一张表根据指定 key 分组,并执行双重统计

資深大佬 : jss 1

根据商户 ID 分组,同时统计出order_type = 1消费金额money和order_type = 2提现金额money

表结构
DROP TABLE IF EXISTS `store_money_record`; CREATE TABLE `store_money_record` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `order_no` char(40) NOT NULL,   `store_id` int(11) NOT NULL DEFAULT '0',   `money` decimal(10,2) DEFAULT '0.00',   `content` char(60) NOT NULL,   `type` tinyint(1) NOT NULL DEFAULT '0',   `order_id` int(11) DEFAULT '0',   `order_type` tinyint(1) DEFAULT '0',   `create_time` int(11) DEFAULT NULL,   `update_time` int(11) DEFAULT NULL,   `delete_time` int(11) DEFAULT NULL,   PRIMARY KEY (`id`),   UNIQUE KEY `order_no` (`order_no`),   KEY `idx_delete_time` (`delete_time`),   KEY `idx_store_id` (`store_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 

大佬有話說 (7)

  • 資深大佬 : asAnotherJack

    select store_id, sum(if(order_type=1,money,0)) as money1, sum(if(order_type=2,money,0)) as money2 from store_money_record group by store_id

  • 主 資深大佬 : jss

    @asAnotherJack 谢谢

  • 主 資深大佬 : jss

    @asAnotherJack 还是这个表 根据商户 ID 分组,同时统计出 order_type = 1 消费数量和 order_type = 2 提现数量 怎么写?

  • 資深大佬 : asAnotherJack

    @jss #3 消费数量指什么,次数吗? count(if(order_type = 1,1,null))

  • 主 資深大佬 : jss

    @asAnotherJack 我没查到这块文档,一直搞不搞懂 sum(if(order_type=1,money,0)) 中 if 里的参数 含义; 那个 0 /null 在什么使用? 因为,我之前进行 count(if(order_type = 1,1,0)) 时,查询结果不正确 ,将 0 改成 null 就对了。

  • 資深大佬 : asAnotherJack

    @jss #5 搜 count if sum if 网上有很多

  • 主 資深大佬 : jss

    @asAnotherJack 谢谢

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具