简体   繁体   English

如何在MySQL中合并多行数据

[英]How to combine data in multiple rows in mysql

I'm attempting to combine data in MySQL into a view or stored procedure to either write back out to a new table or access as a view. 我试图将MySQL中的数据合并到视图或存储过程中,以写回新表或以视图访问。 My database stores customer meta in a separate line with keys and values. 我的数据库将客户元数据存储在带有键和值的单独行中。 I'm puzzled as to how to extract this data in a useful form. 我对如何以有用的形式提取这些数据感到困惑。

My data is formed this way: 我的数据是这样形成的:

 id order       customer    type        key               value
1   42FF86A1    45858007    shipping    address-name      David Customer
2   42FF86A1    45858007    shipping    email-address     david@email.com
3   42FF86A1    45858007    shipping    number            2125551212
4   42FF86A1    45858007    shipping    address-line1     5353 My Street
5   42FF86A1    45858007    shipping    address-line2     #2
6   42FF86A1    45858007    shipping    city              MyCity
7   42FF86A1    45858007    shipping    region            CA
8   42FF86A1    45858007    shipping    postal-code       95555
9   42FF86A1    45858007    shipping    country           US

Ultimately I'd like to be able to read the data and export it easily for use in my CRM or Excel. 最终,我希望能够读取数据并轻松导出以供我的CRM或Excel使用。

I've attempted to implement https://stackoverflow.com/a/5967720/3001841 but it just doesn't make sense to me how. 我已经尝试实现https://stackoverflow.com/a/5967720/3001841,但这对我来说没有任何意义。

Simply getting the data in a single row would be useful to me where: 简单地将数据放在一行中对我有帮助:

customer,order,address-name,address-line1,address-line2,city,region,postal-code,country,email-address,number 客户,订单,地址名称,地址行1,地址行2,城市,区域,邮政编码,国家,电子邮件地址,号码

Here's an example that uses a view for convenience, but you can bake the view query into the final query as well if you want. 这是一个使用视图的示例,为方便起见,但您也可以根据需要将视图查询烘烤到最终查询中。 (ive used invoice where you've used order , sorry. incidentally, try to avoid using mysql reserved words as field names, its painful having to remember to backtick everything.) (很抱歉,在您使用过order地方使用了invoice ,顺便说一句,请避免使用mysql保留字作为字段名,必须记住对所有内容进行重新标记。)

create view order_pivot as (
  select customer,  invoice, type, 
  case when `key` = 'address-name' then `value` end as address,
  case when `key` = 'email-address' then `value` end as email,
  case when `key` = 'number' then `value` end as number,
  case when `key` = 'address-line1' then `value` end as addressline1,
  case when `key` = 'address-line2' then `value` end as addressline2,
  case when `key` = 'city' then `value` end as city,
  case when `key` = 'region' then `value` end as region,
  case when `key` = 'postal-code' then `value` end as postalcode,
  case when `key` = 'country' then `value` end as country
  from orders
);

The view transforms the data into rows with columns that match the number of columns we're interested in. It's not very flexible insofar as it requires you to hardcode those key fields, but it does work. 该视图将数据转换成具有与我们感兴趣的列数相匹配的列的行。它不是很灵活,因为它要求您对那些关键字段进行硬编码,但是它确实可以工作。 It gives us one value, and a whole bunch of nulls for each row - so obviously we need to coalesce them all into a single row, that's where the next query comes in. 它给我们一个值,每一行都有一大堆空值-因此显然我们需要将它们全部合并为一行,这是下一个查询出现的地方。

select customer, max(address) addr, max(email) email, max(number) number, max(addressline1) a1, max(addressline2) a2, max(city) city, max(region) region, max(postalcode) postcode, max(country) country
  from order_pivot
  group by customer;

We group by the customer (you would probably want to put a where filter on invoice as well here, and then we can use max() to make sure we ignore all the null values, and grab only the field with valid data. 我们按客户分组(您可能也希望在invoice上放置一个where过滤器,然后我们可以使用max()来确保忽略所有空值,并仅获取具有有效数据的字段。

here's a fiddle 这是一个小提琴

edit 编辑

combining the two queries since you lack view permissions it seems: 由于您缺乏查看权限,因此将两个查询结合起来似乎是:

select customer, max(address) addr, max(email) email, max(number) number, max(addressline1) a1, max(addressline2) a2, max(city) city, max(region) region, max(postalcode) postcode, max(country) country
  from (
  select customer,  invoice, type, 
  case when `key` = 'address-name' then `value` end as address,
  case when `key` = 'email-address' then `value` end as email,
  case when `key` = 'number' then `value` end as number,
  case when `key` = 'address-line1' then `value` end as addressline1,
  case when `key` = 'address-line2' then `value` end as addressline2,
  case when `key` = 'city' then `value` end as city,
  case when `key` = 'region' then `value` end as region,
  case when `key` = 'postal-code' then `value` end as postalcode,
  case when `key` = 'country' then `value` end as country
  from orders
) q
  group by customer;

updated fiddle with combined query 使用组合查询更新小提琴

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

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