mysql row to column analysis

Scenario: Mysq converts ranks and columns, and displays personal and subject scores in a row to facilitate viewing of personal conditions. (For sql, it turns a repeating column and its corresponding column into a row display )

===》

sql test statement

DROP TABLE IF EXISTS `t_score`;
CREATE TABLE `t_score` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) CHARACTER SET utf8 NOT NULL,
  `Subject` varchar(10) CHARACTER SET utf8 NOT NULL,
  `Fraction` double DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of t_score
-- ----------------------------
INSERT INTO `t_score` VALUES ('1', '王海', '语文', '86');
INSERT INTO `t_score` VALUES ('2', '王海', '数学', '83');
INSERT INTO `t_score` VALUES ('3', '王海', '英语', '93');
INSERT INTO `t_score` VALUES ('4', '陶俊', '语文', '88');
INSERT INTO `t_score` VALUES ('5', '陶俊', '数学', '84');
INSERT INTO `t_score` VALUES ('6', '陶俊', '英语', '94');
INSERT INTO `t_score` VALUES ('7', '刘可', '语文', '80');
INSERT INTO `t_score` VALUES ('8', '刘可', '数学', '86');
INSERT INTO `t_score` VALUES ('9', '刘可', '英语', '88');
INSERT INTO `t_score` VALUES ('10', '李春', '语文', '89');
INSERT INTO `t_score` VALUES ('11', '李春', '数学', '80');
INSERT INTO `t_score` VALUES ('12', '李春', '英语', '87');
INSERT INTO `t_score` VALUES ('13', '王海', '物理', '4');

Write conversion sql like this

select 
  `name`,
       sum(if(Subject='语文',Fraction,0)) as 语文,
       sum(if(Subject='英语',Fraction,0)) as 英语,
       sum(if(Subject='数学',Fraction,0))as 数学
        from t_score group by name ;

Show data first

Conversion logic analysis, SQL is first grouped according to group by name, for example, after Wang Hai grouping is

1.name: Wang Hai, Subject merged, Fraction merged

2. if(Subject='language',Fraction,0) traverse. subject judges whether the current subject is Chinese, if it is equal, output the value corresponding to Fraction, if it is not equal, output 0 ,

Taking Chinese as an example, compared with Wang Hai’s other subjects, in order: Chinese and Chinese, Chinese and Mathematics, Chinese and English, Chinese and Physics

The order is 86, 0, 0, 0

3.sum(if(Subject='语文',Fraction,0)) as language, and then add all the results to 86

4. So there will be such a situation ,

(1) For example, there are multiple languages

===" Language and Language, Language and Mathematics, Language and English, Language and Literature

The result is 86, 0, 0, 4 so the sum is 90

(2) sum(if(Subject='语文',Fraction,1000)) does not exist and the default value is 1000, which will increase the normal result

select 
  `name`,
       sum(if(Subject='语文',Fraction,1000)) as 语文,
       sum(if(Subject='英语',Fraction,0)) as 英语,
       sum(if(Subject='数学',Fraction,0))as 数学
        from t_score group by name ;

Result: The language has increased by 2000

Analysis, Wang Hai’s, Chinese and Chinese, Chinese and Mathematics, Chinese and English, Chinese and Chinese The result is 86, 1000, 1000, 4, so add 2090  

Guess you like

Origin blog.csdn.net/Mint6/article/details/94363684