【问题标题】:How to combine data in multiple rows in mysql如何在mysql中合并多行数据
【发布时间】:2015-06-21 08:37:54
【问题描述】:

我正在尝试将 MySQL 中的数据合并到一个视图或存储过程中,以便写回新表或作为视图访问。我的数据库将客户元数据存储在带有键和值的单独行中。我对如何以有用的形式提取这些数据感到困惑。

我的数据是这样形成的:

 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

最终,我希望能够读取数据并将其轻松导出,以便在我的 CRM 或 Excel 中使用。

我已经尝试实现https://stackoverflow.com/a/5967720/3001841,但它对我来说没有意义。

仅在一行中获取数据对我很有用,其中:

客户、订单、地址名称、地址行1、地址行2、城市、地区、邮政编码、国家、电子邮件地址、号码

【问题讨论】:

  • 您能添加您认为“有用的形式”吗?即您希望数据如何显示的示例?以键为列,以值为对应数据的单行?
  • 已编辑。感谢您要求澄清
  • Viswanath - 由于“关键”和价值,我不相信这会起作用。这将简单地连接数据,但它不像示例那样排列整齐

标签: mysql stored-procedures


【解决方案1】:

为了方便起见,这是一个使用视图的示例,但您也可以根据需要将视图查询烘焙到最终查询中。 (我使用了invoice,而你使用了order,对不起。顺便说一句,尽量避免使用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
);

视图将数据转换为列数与我们感兴趣的列数相匹配的行。它不是很灵活,因为它需要您对这些关键字段进行硬编码,但它确实有效。它为我们提供了一个值,以及每一行的一大堆空值 - 所以显然我们需要将它们全部合并到一行中,这就是下一个查询的来源。

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;

我们按客户分组(您可能希望在这里也将where 过滤器放在invoice 上,然后我们可以使用max() 确保我们忽略所有空值,并只抓取具有有效数据的字段。

here's a fiddle

编辑

组合这两个查询,因为您似乎缺乏查看权限:

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

【讨论】:

  • 谢谢。我已经尝试了您的代码,但现在我收到一个错误:错误代码:1142. SELECT 命令拒绝用户 'myusername'@'myip' 用于表 'order_pivot' 我正在跟踪我们的数据库管理员以了解它的原因抛出此错误。
  • 似乎您缺乏对视图的选择权限,很有趣。我只是将这两个查询结合起来让您跳过视图 - 它只是有点混乱,但应该仍然可以工作
  • 太棒了!!这真是太棒了。 感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2012-01-28
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 2010-11-18
  • 1970-01-01
相关资源
最近更新 更多