MySQL horizontal and vertical table conversion operation implementation method

MySQL horizontal and vertical table conversion operation implementation method

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:
  • Mysql vertical table conversion to horizontal table method and optimization tutorial

<<:  Vue+Openlayer uses modify to modify the complete code of the element

>>:  Example of troubleshooting method to solve Nginx port conflict

Recommend

Win10 configuration tomcat environment variables tutorial diagram

Before configuration, we need to do the following...

Four data type judgment methods in JS

Table of contents 1. typeof 2. instanceof 3. Cons...

Vue custom encapsulated button component

The custom encapsulation code of the vue button c...

Detailed explanation of webpage screenshot function in Vue

Recently, there is a requirement for uploading pi...

How to quickly install Nginx in Linux

Table of contents What is nginx 1. Download the r...

Basic usage examples of listeners in Vue

Table of contents Preface 1. Basic usage of liste...

Detailed explanation of Javascript basics loop

Table of contents cycle for for-in for-of while d...

Tutorial on using Webpack in JavaScript

Table of contents 0. What is Webpack 1. Use of We...

CSS3 realizes draggable Rubik's Cube 3D effect

Mainly used knowledge points: •css3 3d transforma...

Nginx+FastDFS to build an image server

Installation Environment Centos Environment Depen...

Use html-webpack-plugin' to generate HTML page plugin in memory

When we package the webpackjs file, we introduce ...

Detailed steps to install VMware Tools from scratch (graphic tutorial)

VMware Tools is a tool that comes with VMware vir...

Will mysql's in invalidate the index?

Will mysql's IN invalidate the index? Won'...