MySQL table transverse transpose SQL statement

Source: Internet
Author: User

ID, student, course, mark
1, Zhang San, Chinese, 90
2, John Doe, Chinese, 80
3, Zhang San, mathematics, 70
4, John Doe, mathematics, 80

Now you want to use the Transpose table method to go to the two-dimensional table output, the required format is:

Student, Chinese, maths
Zhang San, 90, 70
John Doe, 80, 80



select student, 
    sum(if(course=‘语文‘,mark,0)) as 语文, 
    sum(if(course=‘数学‘,mark,0)) as 数学
from table1 
group by student

1 A
2 b
1 c
2 D
Merge to
1 a,c
2 B,d

 

select group_concat(column_b,separator ‘,‘) from table_name group by column_a;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.