[英]MySQL - How to unpivot columns to rows?
I'm probably not seeing things very clear at this moment, but I have a table in MySQL which looks like this:我现在可能看的不是很清楚,但我在 MySQL 中有一个表,如下所示:
ID | a | b | c
1 | a1 | b1 | c1
2 | a2 | b2 | c2
For some reason (actually a join on another table - based on ID
, but I think if someone can help me out with this part, I can do the rest myself), I needed those rows to be like this instead:出于某种原因(实际上是另一个表上的连接 - 基于ID
,但我认为如果有人可以帮助我完成这部分,我可以自己完成其余的工作),我需要这些行是这样的:
1 | a1 | a
1 | b1 | b
1 | c1 | c
2 | a2 | a
2 | b2 | b
2 | c2 | c
So basically, I need to view the rows like: ID
, columntitle
, value
Is there any way to do this easily?所以基本上,我需要查看如下行: ID
, columntitle
, value
有没有办法轻松做到这一点?
You are trying to unpivot the data.您正在尝试取消数据透视。 MySQL does not have an unpivot function, so you will have to use a UNION ALL
query to convert the columns into rows: MySQL 没有 unpivot 功能,因此您必须使用UNION ALL
查询将列转换为行:
select id, 'a' col, a value
from yourtable
union all
select id, 'b' col, b value
from yourtable
union all
select id, 'c' col, c value
from yourtable
See SQL Fiddle with Demo .请参阅SQL Fiddle with Demo 。
This can also be done using a CROSS JOIN
:这也可以使用CROSS JOIN
来完成:
select t.id,
c.col,
case c.col
when 'a' then a
when 'b' then b
when 'c' then c
end as data
from yourtable t
cross join
(
select 'a' as col
union all select 'b'
union all select 'c'
) c
It took a long time coming, but MySQL version 8.0.14 finally added support for lateral joins - the official terminology is lateral derived tables .等了很久,MySQL 8.0.14 版本终于加入了对横向联接的支持——官方术语是横向派生表。
This is a very powerful feature, that comes handy in multiple situations, including unpivoting table columns to rows.这是一个非常强大的功能,在多种情况下都派上用场,包括将表列反透视为行。
You can phrase the query as follows:您可以按如下方式表达查询:
select t.id, x.*
from mytable t
cross join lateral (
select a, 'a'
union all select b, 'b'
union all select c, 'c'
) as x(col1, col2)
It may look like this is not a big difference compared to the typical cannonical solution - after all, we are still using union all
within the lateral derived table... But don't get it wrong: this query scans the table only once , as opposed to the other approach, which requires one scan for each column to unpivot.与典型的规范解决方案相比,这看起来可能没有太大区别 - 毕竟,我们仍在横向派生表中使用union all
......但不要误会:这个查询只扫描表一次,与另一种方法相反,后者需要对每一列进行一次扫描才能取消旋转。 So this is more efficient - and the performance gain increases dramatically as the table goes bigger and/or more columns need to be unpivoted.所以这更有效 - 随着表变大和/或更多列需要取消旋转,性能增益会显着增加。
Bottom line: if you are running MySQL 8.0.14 or higher, just use this technique.底线:如果您运行的是 MySQL 8.0.14 或更高版本,请使用此技术。 From that version onwards, this is the canonical way to unpivot in MYSQL.从那个版本开始,这是在 MYSQL 中取消透视的规范方式。
Demo on DB Fiddle : DB Fiddle 上的演示:
Sample data:样本数据:
ID | a | b | c -: | :- | :- | :- 1 | a1 | b1 | c1 2 | a2 | b2 | c2
Query results:查询结果:
id | col1 | col2 -: | :--- | :--- 1 | a1 | a 1 | b1 | b 1 | c1 | c 2 | a2 | a 2 | b2 | b 2 | c2 | c
Side note边注
MySQL 8.0.19 added support for the VALUES
statement , which could help further shortening the query by removing the need to use union all
in a subquery (although I don't see any performance gain here, this makes the query neater). MySQL 8.0.19 添加了对VALUES
语句的支持,这可以通过消除在子查询中使用union all
的需要来帮助进一步缩短查询(尽管我在这里没有看到任何性能提升,这使查询更简洁)。
nfortunately, As of version 8.0.21, this does not work yet - which might be considered a bug - but maybe will in a future version...:不幸的是,从 8.0.21 版开始,这还不起作用- 这可能被认为是一个错误 - 但可能会在未来的版本中......:
select t.id, x.*
from mytable t
cross join lateral (values
row(a, 'a'),
row(b, 'b'),
row(c, 'c')
) as x(col1, col2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.