Home >Database >Mysql Tutorial >How to change the vertical to horizontal arrangement in mysql

How to change the vertical to horizontal arrangement in mysql

(*-*)浩
(*-*)浩Original
2019-05-07 17:33:227224browse

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;

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;

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!

Statement:
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