简体   繁体   English

需要将MySql中的多行或Access与一行合并

[英]Need to combine multiple rows in MySql or Access with one row

I have a table that has a sku number, attribute_name and a attribute value.我有一个包含 sku 编号、attribute_name 和属性值的表。 Each sku could have many attritbutes with it.每个 sku 都可以有很多属性。

Right now the table looks like this.现在桌子看起来像这样。

SKU,             ATT_NM,                                          ATT_VAL
TOST3580,   Supply Type,                                    Toner
TOST3580,   Pre-Consumer Recycled Content Percent [Nom],    0 %
TOST3580,   Post-Consumer Recycled Content Percent [Nom],   0 %
TOST3580,   Total Recycled Content Percent [Nom],           0 %
TOST3580,   OEM/Compatible,                                 OEM

I need them to be more like this.我需要他们更像这样。

SKU,ATT_NM,ATT_VAL,ATT_NM1,ATT_VAL1
TOST3580,Supply,Type,Toner,Pre-Consumer Recycled Content Percent,0 %

adding each attribute value.添加每个属性值。 so for instance the example above would have 5 different attributes.因此,例如上面的示例将具有 5 个不同的属性。 Each SKU has anywhere from 2-20 attributes.每个 SKU 具有 2-20 个属性。 Any help would be awesome.任何帮助都是极好的。 Sorry for the bad formatting.抱歉格式错误。

This can be done in either Access or MySQL这可以在 Access 或 MySQL 中完成

You could try something like this (with MySQL) -你可以尝试这样的事情(使用 MySQL)-

SELECT
    SKU,
    GROUP_CONCAT(IF(ATT_NM = 'Supply Type', ATT_VAL, NULL)) AS `Supply Type`,
    GROUP_CONCAT(IF(ATT_NM = 'Pre-Consumer Recycled Content Percent [Nom]', ATT_VAL, NULL)) AS `Pre-Consumer Recycled Content Percent [Nom]`,
    ...
FROM table
GROUP BY SKU

You could retrieve the list of attribs using -您可以使用 - 检索属性列表

SELECT DISTINCT ATT_NM
FROM table

and then use the values returned to build the prev query.然后使用返回的值构建上一个查询。

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

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