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;
![]() | 1 asAnotherJack 2020-05-12 14:25:19 +08:00 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 |
![]() | 2 jss OP @asAnotherJack 谢谢 |
![]() | 3 jss OP @asAnotherJack 还是这个表 根据商户 ID 分组,同时统计出 order_type = 1 消费数量和 order_type = 2 提现数量 怎么写? |
![]() | 4 asAnotherJack 2020-05-14 16:44:20 +08:00 @jss #3 消费数量指什么,次数吗? count(if(order_type = 1,1,null)) |
![]() | 5 jss OP @asAnotherJack 我没查到这块文档,一直搞不搞懂 sum(if(order_type=1,money,0)) 中 if 里的参数 含义; 那个 0 /null 在什么使用? 因为,我之前进行 count(if(order_type = 1,1,0)) 时,查询结果不正确 ,将 0 改成 null 就对了。 |
![]() | 6 asAnotherJack 2020-05-14 17:24:51 +08:00 @jss #5 搜 count if sum if 网上有很多 |
![]() | 7 jss OP @asAnotherJack 谢谢 |