gpt4 book ai didi

mysql 根据值将一列分成多列

转载 作者:行者123 更新时间:2023-11-29 14:26:09 25 4
gpt4 key购买 nike

mysql> select description from devices where id=172;
+--------------------------------------------------------------------------------+
| description |
+--------------------------------------------------------------------------------+
| Fault: 'HYD OIL TEMP HIGH' from 'Controller' of type 'SYSTEM' with code '1003' |
+--------------------------------------------------------------------------------+
1 row in set (0.01 sec)

以上是我的数据库中记录的模式。我正在尝试将它们分成多个列,这样只是为了运行报告。

+---------+------------------------+--------------+---------+---------+
| status | description | device | system | code |
+---------+------------------------+--------------+---------+---------+
| Fault | HYD HYD OIL TEMP HIGH | controller | SYSTEM | 1003 |
+---------+------------------------+--------------+---------+---------+

我知道我正在做的上述事情更糟糕。我不想更改 ETL 来运行一次性报告。这是我尝试过的。

mysql> select substr(description, 1, locate(":", description)-1) as status from devices where id=172;
+--------+
| status |
+--------+
| Fault |
+--------+
1 row in set (0.00 sec)

数据库位于亚马逊 RDS 上。所以我不能使用lib_mysqludf_preg 。我只能执行简单的 SQL。感谢帮助。谢谢。

最佳答案

试试这个 -

SELECT
SUBSTRING_INDEX(description, ':', 1) status,
SUBSTRING_INDEX(SUBSTRING_INDEX(description, '''', 2), '''', -1) description,
SUBSTRING_INDEX(SUBSTRING_INDEX(description, '''', 4), '''', -1) device,
SUBSTRING_INDEX(SUBSTRING_INDEX(description, '''', 6), '''', -1) system,
SUBSTRING_INDEX(SUBSTRING_INDEX(description, '''', 8), '''', -1) code
FROM
devices
WHERE
id = 172;

关于mysql 根据值将一列分成多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10925585/

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