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

Vue implements countdown function

This article example shares the specific code of ...

Example of how to configure multiple virtual hosts in nginx

It is very convenient to configure virtual host v...

Implementation of the Pycharm installation tutorial on Ubuntu 18.04

Method 1: Download Pycharm and install Download a...

VMware configuration VMnet8 network method steps

Table of contents 1. Introduction 2. Configuratio...

VMware vSAN Getting Started Summary

1. Background 1. Briefly introduce the shared sto...

Detailed explanation of MYSQL log and backup and restore issues

This article shares MYSQL logs and backup and res...

Detailed explanation of the relationship between Linux and GNU systems

Table of contents What is the Linux system that w...

Javascript asynchronous programming: Do you really understand Promise?

Table of contents Preface Basic Usage grammar Err...

How to query a record in Mysql in which page of paging

Preface In practice, we may encounter such a prob...

React implements the sample code of Radio component

This article aims to use the clearest structure t...

Introduction to HTML_PowerNode Java Academy

What is HTML? HTML is a language used to describe...

A practical record of restoring a MySQL Slave library

Description of the situation: Today, I logged int...

Navicat for MySQL tutorial

First, you need to download and install Navicat f...

Example of how to deploy MySQL 8.0 using Docker

1. Refer to the official website to install docke...