MySQL rows into columns and columns rows column transfer switch

MySQL row and column switch to turn columns

 

Line transfer column

For example: FIG. 1 is converted into the FIG. 2 shows the results of

figure 1 

 

figure 2

 

1
2
3
4
5
6
7
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  DEFAULT  CHARSET=utf8;

 

1
2
3
4
5
6
7
8
9
10
insert  into  TEST_TB_GRADE(USER_NAME, COURSE, SCORE)   values
( "张三" "数学" , 34),
( "张三" "语文" , 58),
( "张三" "英语" , 58),
( "李四" "数学" , 45),
( "李四" "语文" , 87),
( "李四" "英语" , 45),
( "王五" "数学" , 76),
( "王五" "语文" , 34),
( "王五" "英语" , 89);

  

Line transfer columns SQL:

1
2
3
4
5
6
SELECT  user_name ,
     MAX ( CASE  course  WHEN  '数学'  THEN  score  ELSE  END  ) 数学,
     MAX ( CASE  course  WHEN  '语文'  THEN  score  ELSE  END  ) 语文,
     MAX ( CASE  course  WHEN  '英语'  THEN  score  ELSE  END  ) 英语
FROM  test_tb_grade
GROUP  BY  USER_NAME;

  

Column switch

For example: to convert the FIG. 1 FIG. 3 shows the results of

image 3

 

Column switch to SQL:

1
2
3
4
select  user_name,  '语文'  COURSE , CN_SCORE  as  SCORE  from  test_tb_grade2
union  select  user_name,  '数学'  COURSE, MATH_SCORE  as  SCORE  from  test_tb_grade2
union  select  user_name,  '英语'  COURSE, EN_SCORE  as  SCORE  from  test_tb_grade2
order  by  user_name,COURSE;

 

[Reprinted from: http: //blog.csdn.net/jx_870915876/article/details/52403472]

Line transfer column

For example: FIG. 1 is converted into the FIG. 2 shows the results of

figure 1 

 

figure 2

 

1
2
3
4
5
6
7
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  DEFAULT  CHARSET=utf8;

 

1
2
3
4
5
6
7
8
9
10
insert  into  TEST_TB_GRADE(USER_NAME, COURSE, SCORE)   values
( "张三" "数学" , 34),
( "张三" "语文" , 58),
( "张三" "英语" , 58),
( "李四" "数学" , 45),
( "李四" "语文" , 87),
( "李四" "英语" , 45),
( "王五" "数学" , 76),
( "王五" "语文" , 34),
( "王五" "英语" , 89);

  

Line transfer columns SQL:

1
2
3
4
5
6
SELECT  user_name ,
     MAX ( CASE  course  WHEN  '数学'  THEN  score  ELSE  END  ) 数学,
     MAX ( CASE  course  WHEN  '语文'  THEN  score  ELSE  END  ) 语文,
     MAX ( CASE  course  WHEN  '英语'  THEN  score  ELSE  END  ) 英语
FROM  test_tb_grade
GROUP  BY  USER_NAME;

  

Column switch

For example: to convert the FIG. 1 FIG. 3 shows the results of

image 3

 

Column switch to SQL:

1
2
3
4
select  user_name,  '语文'  COURSE , CN_SCORE  as  SCORE  from  test_tb_grade2
union  select  user_name,  '数学'  COURSE, MATH_SCORE  as  SCORE  from  test_tb_grade2
union  select  user_name,  '英语'  COURSE, EN_SCORE  as  SCORE  from  test_tb_grade2
order  by  user_name,COURSE;

 

[Reprinted from: http: //blog.csdn.net/jx_870915876/article/details/52403472]

Guess you like

Origin www.cnblogs.com/longsanshi/p/11975830.html