您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

mysql行转列利用casewhen_MySQL

2024/4/27 5:22:04发布6次查看
create table bill (id char(36) not null,customer int(255) null default null comment ‘顾客’,shop int(255) null default null comment ‘消费店铺’,money decimal(10,2) null default null comment ‘花费’,type int(255) null default null comment ‘类型 0’,primary key (id))collate=’utf8_general_ci’engine=innodb;insert into bill (id, customer, shop, money, type) values (‘117f1a3c-ae68-42de-aa29-b9679a9a79f8’, 68, 9, 100.00, 1);insert into bill (id, customer, shop, money, type) values (‘1606dd9a-5e1b-4bb6-9641-7508587aab56’, null, 9, 100.00, 1);insert into bill (id, customer, shop, money, type) values (‘649d86ff-0271-4799-bc3c-173514f40f7c’, null, 9, 300.00, 1);insert into bill (id, customer, shop, money, type) values (‘6d502fb6-9664-4f0f-8e2d-2fc9e21202b3’, 68, 9, 100.00, 1);insert into bill (id, customer, shop, money, type) values (‘7036ba44-8143-4a5b-802f-522b39253572’, 68, 9, 100.00, 1);insert into bill (id, customer, shop, money, type) values (‘7bcb427f-0eb1-4aa7-811c-997d7dffecb1’, 68, 9, 100.00, 3);insert into bill (id, customer, shop, money, type) values (‘8043bd41-54c9-43d1-bf4a-def04e744343’, 68, 16, 180.00, 1);insert into bill (id, customer, shop, money, type) values (‘8fbbcc6c-fcb0-4e95-bfd6-19d2e895694f’, null, 9, 200.00, 1);insert into bill (id, customer, shop, money, type) values (‘94fa7e96-ae4a-423e-9c18-069adf601822’, null, 9, 100.00, 1);insert into bill (id, customer, shop, money, type) values (‘a8388be4-3862-41ca-aa0a-867cb9c9966b’, 68, 9, 0.00, 6);insert into bill (id, customer, shop, money, type) values (‘ec6713c6-4460-44f1-8f32-d4c409571855’, 68, 9, 100.00, 1);create table card_model (id char(36) not null,name varchar(255) null default null,shop int(255) null default null,v1 decimal(10,2) null default null comment ‘参数1’,type int(255) null default null,primary key (id))collate=’utf8_general_ci’engine=innodb;insert into card_model (id, name, shop, v1, type) values (‘af7b7105-b3d0-4552-86a2-f187f4cbaabd’, ‘wedf’, 9, 100.00, 1);insert into card_model (id, name, shop, v1, type) values (‘d7b10362-d189-440b-9d7a-72465078c066’, ‘frm’, 9, 200.00, 2);
希望得到类似这种的。
其中type1是bill表中type=’1’的所有的money的和,其中type2是bill表中type=’2’的所有的money的和,card_model表中所有v1的数据的和全部为’card’类
首先分析:
1、按店铺分组,要分别得到他们的和
第二张图(http://img.blog.csdn.net/20150828214433916)
select ifnull(sum(b.money),0) money,shop,’type1’ type_test from bill b where b.type=1 group by b.shop
union
select ifnull(sum(b.money),0) money,shop,’type2’ type_test from bill b where b.type=2 group by b.shop
union
select ifnull(sum(b.v1),0) money,shop,’card’ type_test from card_model b group by b.shop
从数据看没有type2的数据。
使用ifnull是防止产生null结果,利用别名money,type_test使其获得相同的列不会报错
2、怎样将type1和card还有一个type2转化为列即第一张图
利用case when
select shop ‘店铺’,sum((case type_test when ‘type1’ then money else 0 end)) ‘type1’,sum((case type_test when ‘type2’ then money else 0 end)) ‘type2’,sum((case type_test when ‘card’ then money else 0 end)) ‘card’,sum(money) ‘总和’from(select ifnull(sum(b.money),0) money,shop,’type1’ type_test from bill b where b.type=1 group by b.shopunionselect ifnull(sum(b.money),0) money,shop,’type2’ type_test from bill b where b.type=2 group by b.shopunionselect ifnull(sum(b.v1),0) money,shop,’card’ type_test from card_model b group by b.shop)agroup by shop
这里利用case when 和别名type_test判断再取别名得到想要的结果,最后按shop分组。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录