This article through the SQL sample code to introduce the MySQL database how to achieve row and column conversion, the following words not to say, directly to see the sample code.
Original table:
Table name: User
----------------------------------------
name | course | grade
--------------------- -------------------
Zhangsan | Java |
----------------------------------------
Zhangsan | C + +
----------------------------------------
Lisi | java |
---------------- ------------------------
Lisi | C #
----------------------------------------
Use an SQL statement to get the following form:
----------------------------------------
name | java | C + + | C #----------------------------------------Zhangsan | |- null
------------------- ---------------------Lisi | |- NULL |
----------------------------------------
Programme I
Select name,
sum (case when course= ' Java ' then grade end) as Java,
sum (case when course= ' C + + ' then grade end) a S C + +,
sum (case when course= ' C # ' then grade end) as C # from
test GROUP by name
Programme II
SELECT DISTINCT c. ' Name ' as name,
(select grade from test where name = C. ' name ' and course = ' java ') as Java,
(SE Lect grade from test where name = C. ' name ' and course = ' C + + ') as C + +,
(select grade from test where name = C. ' Name ') and course = ' C # ') as C # from
test C
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can have some help, if there is doubt you can message exchange.