mysql list转表,我如何...在mysql中将列表转换为行副

I have a table like:Brand Price Colour Size

addidas 500 Black 7

Nike 400 Blue 8

Reebok 200 Red 9

I need to transpose it like:

AttributeName AttributeValue

Brand addidas

Price 500

Colour Black

Size 7

Brand Nike

Price 400

Colour Blue

Size 8

Brand Reebok

Price 200

Colour Red

Size 9

What I have tried:

No source still found only found to transpose to row to column vice solutions

解决方案Hello,

As per your requirement I make below solution.Though it is not good approach.

//Create Table

CREATE TABLE a

(

id int,

brand varchar(20) ,

price int,

color varchar(20),

size int

) ;

//Insert rows into table

INSERT INTO a (id,brand,price,color,size)

VALUES

(1,'addidas', 500 ,'Black', 7),

(2,'Nike', 400 ,'Blue' ,8),

(3,'Reebok', 200 ,'Red' ,6) ;

Query to get the required output is

select AttributeName,AttributeValue from

(select a.id,1 as 'colid','brand' as 'AttributeName', brand as 'AttributeValue' from a

union

select a.id,2 as 'colid','price' , price from a

union

select a.id,3 as 'colid','color',color from a

union

select a.id ,4 as 'colid','size',size from a

)x order by x.id,x.colid

Here colid variable is used to get the attribute value by required order.

Out put is available in This link

Thanks

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值