This article will introduce how to use sql statements to change table data from vertical display to horizontal display.
Recommended courses:MySQL Tutorial.
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.
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:
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:
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!