Home > Database > Mysql Tutorial > body text

How to change the vertical to horizontal arrangement in mysql

(*-*)浩
Release: 2020-09-16 10:29:09
Original
7181 people have browsed it

This article will introduce how to use sql statements to change table data from vertical display to horizontal display.

Recommended courses: MySQL Tutorial.

How to change the vertical to horizontal arrangement in mysql

In mysql, the case when statement can be used to query vertical to horizontal. The specific steps are as follows:

The first step is to create a data table , I have created it here, and the following is a display data chart.

How to change the vertical to horizontal arrangement in mysql

The second step is to display the total score of each class vertically, using the following statement:

select class,sum(score) from student group by class;
Copy after login

Query results:

How to change the vertical to horizontal arrangement in mysql

The third step is to display the results horizontally, use the case when statement, the statement is as follows:

select 
sum(case when class='一年一班' then score else 0 end) 一年一班成绩,
sum(case when class='一年二班' then score else 0 end) 一年二班成绩,
sum(case when class='一年三班' then score else 0 end) 一年三班成绩
from student;
Copy after login

Query results:

How to change the vertical to horizontal arrangement in mysql

The above is the detailed content of How to change the vertical to horizontal arrangement in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template