简体   繁体   English

多行成列mysql

[英]multiple rows into columns mysql

Hi I am trying to create a mysql query that will convert multiple rows in a table to unique columns 嗨,我正在尝试创建一个mysql查询,它将表中的多行转换为唯一列

The data I have is as follows: 我的数据如下:

数据库表

The table I would like to see is as follows: 我想看到的表如下:

GEID|Username|First Name|Last Name|Email|Country|Dealer Code

The statement which could be used is 可以使用的语句是

UPDATE table_name
SET column1 = value 1 , column 2 = value 2 ...
Where condition;

Sorry but my SQL isn't the best but hope the statement helps 抱歉,我的SQL不是最好的,但希望该语句对您有所帮助

This is a real pain, because you don't have an id identifying groups that are the same. 这是一个真正的痛苦,因为您没有标识相同组的ID。 In other words, you are missing the entity id. 换句话说,您缺少实体ID。

I think you can construct one by counting the number of GEID values before any given row. 我认为您可以通过在任何给定行之前计算GEID值的数量来构造一个。 The rest is just aggregation: 剩下的只是聚合:

select max(case when fieldname = 'GEID' then fieldData end) as GEID,
       max(case when fieldname = 'Username' then fieldData end) as Username,
       . . .
from (select t.*,
             (select count(*) from t t2 where t2.id <= t.id and t2.fieldName = 'GEID'
             ) as grp
      from t
     ) t
group by grp;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM