This article uses examples to illustrate how to implement the conversion operations between MySQL horizontal and vertical tables. Share with you for your reference, the details are as follows: First create a score table (vertical table) create table user_score ( name varchar(20), subjects varchar(20), score int ); insert into user_score(name,subjects,score) values('张三','语文',60); insert into user_score(name,subjects,score) values('张三','数学',70); insert into user_score(name,subjects,score) values('张三','英语',80); insert into user_score(name,subjects,score) values('李四','语文',90); insert into user_score(name,subjects,score) values('李四','数学',100); Create another score table (horizontal table) create table user_score2 ( name varchar(20), yuwen int, shuxue int, yingyu int ); insert into user_score2(name,yuwen,shuxue,yingyu) values('张三',60,70,80); insert into user_score2(name,yuwen,shuxue,yingyu) values('李四',90,100,0); Convert vertical table to horizontal table select name,sum(case subjects when 'Chinese' then score else 0 end) as 'Chinese',sum(case subjects when 'Mathematics' then score else 0 end) as 'Mathematics', sum(case subjects when 'English' then score else 0 end) as 'English'from user_score group by name; Convert vertical table to horizontal table SELECT name,'yuwen' AS subjects,yuwen AS score FROM user_score2 UNION ALL SELECT name,'shuxue' AS subjects,shuxue AS score FROM user_score2 UNION ALL SELECT name,'yingyu' AS subjects,yingyu AS score FROM user_score2 ORDER BY name,subjects DESC; Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary" I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
<<: Vue+Openlayer uses modify to modify the complete code of the element
>>: Example of troubleshooting method to solve Nginx port conflict
Before configuration, we need to do the following...
Table of contents 1. typeof 2. instanceof 3. Cons...
The custom encapsulation code of the vue button c...
Recently, there is a requirement for uploading pi...
Table of contents What is nginx 1. Download the r...
When looking at High Performance MySQL Version 3 ...
Table of contents Preface 1. Basic usage of liste...
Table of contents cycle for for-in for-of while d...
Table of contents 0. What is Webpack 1. Use of We...
Mainly used knowledge points: •css3 3d transforma...
Installation Environment Centos Environment Depen...
Adding the rel="nofollow" attribute to ...
When we package the webpackjs file, we introduce ...
VMware Tools is a tool that comes with VMware vir...
Will mysql's IN invalidate the index? Won'...