简体   繁体   English

所有列到mysql行

[英]All columns to rows mysql

I have a table in mysql called 'prices_x' that looks like this: 我在mysql中有一个名为'prices_x'的表,如下所示:

ID Fecha      Hora  hfmachine1 hcmachinex hfmachiney hfmachinez hfmachinep etc...
1  12/01/01/  00:00 90         100        100        98         78         etc...
2  12/01/02/  01:00 90         100        100        98         78         etc...

and i have other called 'prices_y' with the same columns but with diferents values. 我还有其他名为'prices_y'的列具有相同的列,但具有不同的值。

ID Fecha      Hora  hfmachine1 hcmachinex hfmachiney hfmachinez hfmachinep etc...
1  12/01/01/  00:00 50         40          80        76         89         etc...
2  12/01/02/  01:00 60         40          90        30         23         etc.

I want to do a report page with php but first i need to convert my table in this. 我想用php做一个报告页面,但首先我需要转换我的表格。 only want to show all machines in a date and time specific (i know how to do that), but i don't know how to convert my columns to rows, i'm try everything and i don't find a solution. 只想显示特定日期和时间的所有机器(我知道该怎么做),但我不知道如何将我的列转换为行,我尝试了一切,我找不到解决方案。

Id Machine   prices_x, prices_y
1 hfmachine  90        50
2 hfmachinex 100       40
3 hfmachiney 100       80
4 hfmacinez  98        76
5 hfchinep   78        89

Thanks. 谢谢。

This process that you want to implement is known an unpivot . 您要实现的此过程称为unpivot Unfortunately MySQL does not have any UNPIVOT function but you can use a UNION ALL query to get the result. 不幸的是,MySQL没有任何UNPIVOT函数,但你可以使用UNION ALL查询来获得结果。

The UNION ALL converts the multiple columns into multiple rows. UNION ALL将多列转换为多行。 You can do this for each table and then join the tables on the fecha , hora and column name. 您可以为每个表执行此操作,然后在fechahora和列名称上加入表。 The query would be similar to the following: 该查询将类似于以下内容:

select x.col, 
  x.price_x,
  y.price_y
from
(
  select id, fecha, hora, 'hfmachine1' col, hfmachine1 price_x
  from prices_x
  union all
  select id, fecha, hora, 'hcmachinex' col, hcmachinex price_x
  from prices_x
  union all
  select id, fecha, hora, 'hfmachiney' col, hfmachiney price_x
  from prices_x
  union all
  select id, fecha, hora, 'hfmachinez' col, hfmachinez price_x
  from prices_x
  union all
  select id, fecha, hora, 'hfmachinep' col, hfmachinep price_x
  from prices_x
) x
left join
(
  select id, fecha, hora, 'hfmachine1' col, hfmachine1 price_y
  from prices_y
  union all
  select id, fecha, hora, 'hcmachinex' col, hcmachinex price_y
  from prices_y
  union all
  select id, fecha, hora, 'hfmachiney' col, hfmachiney price_y
  from prices_y
  union all
  select id, fecha, hora, 'hfmachinez' col, hfmachinez price_y
  from prices_y
  union all
  select id, fecha, hora, 'hfmachinep' col, hfmachinep price_y
  from prices_y
) y
  on x.fecha = y.fecha
  and x.hora = y.hora
  and x.col = y.col;

See Demo 演示

If possible my suggestion would be to look at normalizing the tables which would make querying the data significantly easier. 如果可能的话,我的建议是查看规范化表格,这将使查询数据变得更加容易。

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

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