gpt4 book ai didi

MySql 文本行到列(转置)

转载 作者:行者123 更新时间:2023-11-29 18:29:57 24 4
gpt4 key购买 nike

这里有很多例子可以实现相反的效果——从列到行。但不是这个。

这些行的 URL 如下所示:

颜色:黑色http://www.example.com/1/http://www.example.com/2/http://www.example.com/3/http://www.example.com/4/

我需要将它们放入每个网址的单独行中,如下所示:

color           URL
color: black http://www.example.com/1/
color: black http://www.example.com/2/
color: black http://www.example.com/3/
color: black http://www.example.com/4/

有没有办法在 MySql 中实现这一点?

最佳答案

如果这些位于不同的列中,您可以执行以下操作:

select color, url1 as url from t
union all
select color, url2 from t
union all
select color, url3 from t
union all
select color, url4 from t;

以下方法可能有效:

如果它们位于一个困惑的列中(如示例所示),则类似于:

select color,
concat('http:', substring_index(substring_index(concat(url, 'http:'), 'http:', 2), 'http:', -1)) as url
from t
union all
select color,
concat('http:', substring_index(substring_index(concat(url, 'http:'), 'http:', 3), 'http:', -1)) as url
from t
where url like 'http:%http:%'
union all
select color,
concat('http:', substring_index(substring_index(concat(url, 'http:'), 'http:', 4), 'http:', -1)) as url
from t
where url like 'http:%http:%http:%';

您将继续为您拥有的每个网址添加子查询。

关于MySql 文本行到列(转置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45774009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com