Mysql行转列的实现方式有以下几种: 使用CASE语句:通过使用CASE语句可以根据条件将行数据转换为列数据。 SELEC
Mysql行转列的实现方式有以下几种:
SELECT
id,
MAX(CASE WHEN attribute = 'attribute1' THEN value END) AS attribute1,
MAX(CASE WHEN attribute = 'attribute2' THEN value END) AS attribute2,
MAX(CASE WHEN attribute = 'attribute3' THEN value END) AS attribute3
FROM
table
GROUP BY
id;
SELECT
id,
MAX(CASE WHEN attribute = 'attribute1' THEN value END) AS attribute1,
MAX(CASE WHEN attribute = 'attribute2' THEN value END) AS attribute2,
MAX(CASE WHEN attribute = 'attribute3' THEN value END) AS attribute3
FROM
(SELECT id, attribute, value FROM table) AS t
GROUP BY
id;
SELECT
id,
SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN attribute = 'attribute1' THEN value END), ',', 1) AS attribute1,
SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN attribute = 'attribute2' THEN value END), ',', 1) AS attribute2,
SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN attribute = 'attribute3' THEN value END), ',', 1) AS attribute3
FROM
table
GROUP BY
id;
以上是几种常见的Mysql行转列的实现方式,可以根据实际需求选择适合的方式。
--结束END--
本文标题: mysql行转列的实现方式有哪些
本文链接: https://www.lsjlt.com/news/598577.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0