MySQL ranks convert each other

Source: Internet
Author: User

The ranks of each other convert

/*Create a table*/CREATE TABLEIC (NAMEVARCHAR( -), ProductVARCHAR( -), AmountINT);INSERT  intoICVALUES    ('Wang Yi','A1', -),    ('Wang Yi','A3', Max),    ('Wang Yi','A2',Ten),    ('Zhao','A1', the),    ('Zhao','A2', $),    ('Zhao','A3', +),    ('Liu Liu','A1', -),    ('Liu Liu','A2', -),    ('Liu Liu','A3', the);SELECT *  fromIc

To make a column with the value of the product column, the column name becomes NAME,A1,A2,A3.

CREATE TABLEic_1SELECTname,SUM(IF(Product='A1', Amount,0)) asA1,SUM(IF(Product='A2', Amount,0)) asA2,SUM(IF(Product='A3', Amount,0)) asA3 fromICGROUP  byName

If you change the Ic_1 table back to its original appearance

SELECTName'A1'  asProduct, A1 asAmount fromic_1UNIONSELECTName'A2'  asProduct, A2 asAmount fromic_1UNIONSELECTName'A3'  asProduct, A3 asAmount fromic_1ORDER  byName

MySQL ranks convert each other

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.