• sql 纵表转横表


    
    

    DROP TABLE IF EXISTS `course`;
    CREATE TABLE `course` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

    
    

    -- ----------------------------
    -- Records of course
    -- ----------------------------
    INSERT INTO `course` VALUES ('1', '语文');
    INSERT INTO `course` VALUES ('2', '数学');
    INSERT INTO `course` VALUES ('3', '英语');

    
    

    -- ----------------------------
    -- Table structure for score
    -- ----------------------------
    DROP TABLE IF EXISTS `score`;
    CREATE TABLE `score` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `sid` int(11) DEFAULT NULL,
    `cid` int(11) DEFAULT NULL,
    `score` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;

    
    

    -- ----------------------------
    -- Records of score
    -- ----------------------------
    INSERT INTO `score` VALUES ('1', '1', '1', '60');
    INSERT INTO `score` VALUES ('2', '1', '2', '87');
    INSERT INTO `score` VALUES ('3', '1', '3', '64');
    INSERT INTO `score` VALUES ('4', '2', '1', '76');
    INSERT INTO `score` VALUES ('5', '2', '2', '98');
    INSERT INTO `score` VALUES ('6', '2', '3', '73');
    INSERT INTO `score` VALUES ('7', '3', '1', '22');
    INSERT INTO `score` VALUES ('8', '3', '2', '62');
    INSERT INTO `score` VALUES ('9', '3', '3', '48');
    INSERT INTO `score` VALUES ('10', '4', '1', '77');
    INSERT INTO `score` VALUES ('11', '4', '2', '47');
    INSERT INTO `score` VALUES ('12', '4', '3', '65');
    INSERT INTO `score` VALUES ('13', '5', '1', '84');
    INSERT INTO `score` VALUES ('14', '5', '2', '88');
    INSERT INTO `score` VALUES ('15', '5', '3', '75');
    INSERT INTO `score` VALUES ('16', '6', '1', '98');
    INSERT INTO `score` VALUES ('17', '6', '2', '76');
    INSERT INTO `score` VALUES ('18', '6', '3', '66');
    INSERT INTO `score` VALUES ('19', '7', '1', '67');
    INSERT INTO `score` VALUES ('20', '7', '2', '93');
    INSERT INTO `score` VALUES ('21', '7', '3', '74');

    
    

    -- ----------------------------
    -- Table structure for student
    -- ----------------------------
    DROP TABLE IF EXISTS `student`;
    CREATE TABLE `student` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `username` varchar(255) DEFAULT NULL,
    `age` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

    
    

    -- ----------------------------
    -- Records of student
    -- ----------------------------
    INSERT INTO `student` VALUES ('1', '张三', '12');
    INSERT INTO `student` VALUES ('2', '李四', '12');
    INSERT INTO `student` VALUES ('3', '王五', '12');
    INSERT INTO `student` VALUES ('4', '赵柳', '12');
    INSERT INTO `student` VALUES ('5', '田七', null);



    SELECT
    s.username, SUM( CASE s.`name` WHEN '语文' THEN s.score END ) AS yuwne, SUM( CASE s.`name` WHEN '语文' THEN s.score END ) AS shuxue, SUM( CASE s.`name` WHEN '语文' THEN s.score END ) AS yinyu FROM ( SELECT student.username, course.`name`, score.score FROM score JOIN student ON student.id = score.sid JOIN course ON course.id = score.cid ) s GROUP BY s.username
  • 相关阅读:
    FastAPI(八十一)实战开发《在线课程学习系统》接口开发 推荐课程列表与课程点赞
    西门子Protal_TIA SCL编程实例_伪随机数_线性同余法LCG
    西门子Protal_TIA SCL编程实例_排序算法
    pythonmysql数据库连接工具类封装
    python获取当前时间戳
    阿里云centOS安装docker和tomcat
    python读取ini配置文件
    md5加密
    最常见的Java面试题及答案汇总(持续更新)
    2022京东Java面试题汇总
  • 原文地址:https://www.cnblogs.com/songfahzun/p/8687598.html
Copyright © 2020-2023  润新知