mysql行转列利用case when

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);

第一张图(http://img.blog.csdn.net/20150828213643933)

希望得到类似这种的。

其中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

网上搜索下case when用法知道这个是一个选择语句,可以查看(http://blog.csdn.net/yufaw/article/details/7600396)这篇博客的列子

第三张图(http://img.blog.csdn.net/20150828215601788)

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.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

)a

group by shop

这里利用case when 和别名type_test判断再取别名得到想要的结果,最后按shop分组。

最后还可以使用IF语句是行转列,查看(http://blog.csdn.net/zhoushengchao/article/details/7321688

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-17 08:00:57

mysql行转列利用case when的相关文章

MySQL 行转列 -》动态行转列 -》动态行转列带计算

Pivot Table Using MySQL - A Complete Guide | WebDevZoomhttp://webdevzoom.com/pivot-table-using-mysql/ Cross-Tabulation (Pivot Tables) with MySQL - CodeProjecthttps://www.codeproject.com/articles/363339/cross-tabulation-pivot-tables-with-mysql Pivot T

Mysql 行转列

select * from t_zg_asset_statistic_info a where a.BATCH_NO='ss20170123181418' ; 这是原始数据: 这是行转列后的样子: select a.DEBT_TYPE, sum(case WHEN DEBT_TYPE ='0' THEN AMOUNT END) '初债', sum(case WHEN DEBT_TYPE ='1' THEN AMOUNT END) '二级债' , CONVERT(group_concat(DEBT

MySQL行转列与列转行

行转列 例如:把图1转换成图2结果展示 图1 图2 CREATE TABLE `TEST_TB_GRADE` ( `ID` int(10) NOT NULL AUTO_INCREMENT, `USER_NAME` varchar(20) DEFAULT NULL, `COURSE` varchar(20) DEFAULT NULL, `SCORE` float DEFAULT '0', PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEF

MySQL -- 行转列 -- GROUP_CONCAT -- MAX(CASE WHEN THEN)

列转行:利用max(case when then) SELECT `name`, MAX( CASE WHEN course='语文' THEN score END ) AS 语文, MAX( CASE WHEN course='数学' THEN score END ) AS 数学, MAX( CASE WHEN course='英语' THEN score END ) AS 英语 FROM student GROUP BY `name` ; 合并字段显示:利用group_cancat(cour

mysql 行变列(多行变成一行/多行合并成一行/多行合并成多列/合并行)

数据库结构如图: 而我想让同一个人的不同成绩变成此人在这一行不同列上显示出来,此时分为2中展现: 第一种展现如图----[多行变一列](合并后的数据在同一列上): sql如下: select name ,group_concat(sore Separator ';') as score from stu group by name 第二种展现如图----[多行变多列](合并后的数据在不同列上): sql如下: SELECT name , MAX(CASE type WHEN '数学' THEN

mysql 行转列,对列的分组求和,对行求和

CREATE TABLE students( id INT PRIMARY KEY, NAME VARCHAR(11)); CREATE TABLE courses( id INT PRIMARY KEY, NAME VARCHAR(11)); CREATE TABLE scores( sid INT, cid INT, score INT, RIMARY KEY (sid,cid)); 3表关联查询: SELECT st.id,st.name,c.NAME course,sc.score FR

mysql行转换为列

1.建表 CREATE TABLE `t_log_code_num` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `server_id` int(3) NOT NULL,  `date` date NOT NULL,  `code` varchar(20) NOT NULL,  `num` int(10) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8; 2.录入数据 IN

mysql 行转换为列

http://www.cnblogs.com/acelove/archive/2004/11/29/70434.html上提到了一个行列转换的方法,其实在2005里有更可读的写法,就是使用pivot运算符: create table abc (student varchar(50),class varchar(50),grade int)INSERT INTO abcSELECT '孙小美','数学',10 UNION ALLSELECT '孙小美','语文',20 UNION ALLSELEC

mysql 行转换列

http://www.cnblogs.com/acelove/archive/2004/11/29/70434.html上提到了一个行列转换的方法,其实在2005里有更可读的写法,就是使用pivot运算符: create table abc (student varchar(50),class varchar(50),grade int)INSERT INTO abcSELECT '孙小美','数学',10 UNION ALLSELECT '孙小美','语文',20 UNION ALLSELEC