MySQL如何实现行转列分级输出?_MySQL
概述
好久没写SQL语句,今天看到问答中的一个问题,拿来研究一下。
问题链接:关于Mysql 的分级输出问题
情景简介
学校里面记录成绩,每个人的选课不一样,而且以后会添加课程,所以不需要把所有课程当作列。数据表里面数据如下图,使用姓名+课程作为联合主键(有些需求可能不需要联合主键)。本文以MySQL为基础,其他数据库会有些许语法不同。
数据库表数据:
处理后的结果(行转列):
方法一:
这里可以使用Max,也可以使用Sum;
注意第二张图,当有学生的某科成绩缺失的时候,输出结果为Null;
SELECT SNAME, MAX( CASE CNAME WHEN 'JAVA' THEN SCORE END ) JAVA, MAX( CASE CNAME WHEN 'mysql' THEN SCORE END ) mysql FROM stdscore GROUP BY SNAME;
可以在第一个Case中加入Else语句解决这个问题:
SELECT SNAME, MAX( CASE CNAME WHEN 'JAVA' THEN SCORE ELSE 0 END ) JAVA, MAX( CASE CNAME WHEN 'mysql' THEN SCORE ELSE 0 END ) mysql FROM stdscore GROUP BY SNAME;
方法二:
SELECT DISTINCT a.sname, (SELECT score FROM stdscore b WHERE a.sname=b.sname AND b.CNAME='JAVA' ) AS 'JAVA', (SELECT score FROM stdscore b WHERE a.sname=b.sname AND b.CNAME='mysql' ) AS 'mysql' FROM stdscore a
方法三:
DROP PROCEDURE IF EXISTS sp_score; DELIMITER && CREATE PROCEDURE sp_score () BEGIN #课程名称 DECLARE cname_n VARCHAR (20) ; #所有课程数量 DECLARE count INT ; #计数器 DECLARE i INT DEFAULT 0 ; #拼接SQL字符串 SET @s = 'SELECT sname' ; SET count = ( SELECT COUNT(DISTINCT cname) FROM stdscore ) ; WHILE i < count DO SET cname_n = ( SELECT cname FROM stdscore GROUP BY CNAME LIMIT i, 1 ) ; SET @s = CONCAT( @s, ', SUM(CASE cname WHEN ', '\'', cname_n, '\'', ' THEN score ELSE 0 END)', ' AS ', '\'', cname_n, '\'' ) ; SET i = i + 1 ; END WHILE ; SET @s = CONCAT( @s, ' FROM stdscore GROUP BY sname' ) ; #用于调试 #SELECT @s; PREPARE stmt FROM @s ; EXECUTE stmt ; END&& CALL sp_score () ;
处理后的结果(行转列)分级输出:
方法一:
这里可以使用Max,也可以使用Sum;
注意第二张图,当有学生的某科成绩缺失的时候,输出结果为Null;
SELECT SNAME, MAX( CASE CNAME WHEN 'JAVA' THEN ( CASE WHEN SCORE - (select avg(SCORE) from stdscore where CNAME='JAVA') > 20 THEN '优秀' WHEN SCORE - (select avg(SCORE) from stdscore where CNAME='JAVA') > 10 THEN '良好' WHEN SCORE - (select avg(SCORE) from stdscore where CNAME='JAVA') >= 0 THEN '普通' ELSE '较差' END ) END ) JAVA, MAX( CASE CNAME WHEN 'mysql' THEN ( CASE WHEN SCORE - (select avg(SCORE) from stdscore where CNAME='JAVA') > 20 THEN '优秀' WHEN SCORE - (select avg(SCORE) from stdscore where CNAME='JAVA') > 10 THEN '良好' WHEN SCORE - (select avg(SCORE) from stdscore where CNAME='JAVA') >= 0 THEN '普通' ELSE '较差' END ) END ) mysql FROM stdscore GROUP BY SNAME;
方法二:
SELECT DISTINCT a.sname, (SELECT ( CASE WHEN SCORE - (select avg(SCORE) from stdscore where CNAME='JAVA') > 20 THEN '优秀' WHEN SCORE - (select avg(SCORE) from stdscore where CNAME='JAVA') > 10 THEN '良好' WHEN SCORE - (select avg(SCORE) from stdscore where CNAME='JAVA') >= 0 THEN '普通' ELSE '较差' END ) FROM stdscore b WHERE a.sname=b.sname AND b.CNAME='JAVA' ) AS 'JAVA', (SELECT ( CASE WHEN SCORE - (select avg(SCORE) from stdscore where CNAME='JAVA') > 20 THEN '优秀' WHEN SCORE - (select avg(SCORE) from stdscore where CNAME='JAVA') > 10 THEN '良好' WHEN SCORE - (select avg(SCORE) from stdscore where CNAME='JAVA') >= 0 THEN '普通' ELSE '较差' END ) FROM stdscore b WHERE a.sname=b.sname AND b.CNAME='mysql' ) AS 'mysql' FROM stdscore a
DROP PROCEDURE IF EXISTS sp_score; DELIMITER && CREATE PROCEDURE sp_score () BEGIN #课程名称 DECLARE cname_n VARCHAR (20) ; #所有课程数量 DECLARE count INT ; #计数器 DECLARE i INT DEFAULT 0 ; #拼接SQL字符串 SET @s = 'SELECT sname' ; SET count = ( SELECT COUNT(DISTINCT cname) FROM stdscore ) ; WHILE i < count DO SET cname_n = ( SELECT cname FROM stdscore GROUP BY CNAME LIMIT i, 1 ) ; SET @s = CONCAT( @s, ', MAX(CASE cname WHEN ', '\'', cname_n, '\'', ' THEN ( CASE WHEN SCORE - (select avg(SCORE) from stdscore where CNAME=\'',cname_n,'\') > 20 THEN \'优秀\' WHEN SCORE - (select avg(SCORE) from stdscore where CNAME=\'',cname_n,'\') > 10 THEN \'良好\' WHEN SCORE - (select avg(SCORE) from stdscore where CNAME=\'',cname_n,'\') >= 0 THEN \'普通\' ELSE \'较差\' END ) END)', ' AS ', '\'', cname_n, '\'' ) ; SET i = i + 1 ; END WHILE ; SET @s = CONCAT( @s, ' FROM stdscore GROUP BY sname' ) ; #用于调试 #SELECT @s; PREPARE stmt FROM @s ; EXECUTE stmt ; END&& CALL sp_score ();
几种方法比较分析
第一种使用了分组,对每个课程分别处理。第二种方法使用了表连接。
第三种使用了存储过程,实际上可以是第一种或第二种方法的动态化,先计算出所有课程的数量,然后对每个分组进行课程查询。这种方法的一个最大的好处是当新增了一门课程时,SQL语句不需要重写。
小结
关于行转列和列转行这个概念似乎容易弄混,有人把行转列理解为列转行,有人把列转行理解为行转列;
这里做个定义:
行转列:把表中特定列(如本文中的:CNAME)的数据去重后做为列名(如查询结果行中的“JAVA,mysql”,处理后是做为列名输出);
列转行:可以说是行转列的反转,把表中特定列(如本文处理结果中的列名“JAVA,mysql”)做为每一行数据对应列“CNAME”的值;
关于效率
不知道有什么好的生成模拟数据的方法或工具,麻烦小伙伴推荐一下,抽空我做一下对比;
还有其它更好的方法吗?
本文使用的几种方法应该都有优化的空间,特别是使用存储过程的话会更加灵活,功能更强大;
本文的分级只是给出一种思路,分级的方法如果学生的成绩相差较小的话将失去意义;
如果小伙伴有更好的方法,还请不吝赐教,感激不尽!
有些需求可能不需要联合主键
有些需求可能不需要联合主键,因为一门课程可能允许学生考多次,取最好的一次成绩,或者取多次的平均成绩。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.
