gpt4 book ai didi

mysql - 如何将一列拆分为两列

转载 作者:行者123 更新时间:2023-11-29 04:32:39 31 4
gpt4 key购买 nike

我对名为“电影”的表格有疑问。我发现日期和电影标题都在标题栏中。如图所示:

Sample

我不知道如何处理这类问题。因此,我尝试使用这段代码使其类似于 MySQL 代码,但无论如何我都没有工作。

DataFrame(row.str.split(' ',-1).tolist(),columns = ['title','date'])

如何将它分成两列(标题、日期)?

最佳答案

如果您使用的是 MySQL 8+,那么我们可以尝试使用 REGEXP_REPLACE:

SELECT
REGEXP_REPLACE(title, '^(.*)\\s\\(.*$', '$1') AS title,
REGEXP_REPLACE(title, '^.*\\s\\((\\d+)\\)$', '$1') AS date
FROM yourTable;

Demo

这是一个可以匹配您的标题字符串的通用正则表达式模式:

^.*\s\((\d+)\)$

解释:

^            from the start of the string
(.*)\s match and capture anything, up to the last space
\( match a literal opening parenthesis
(\d+) match and capture the year (any number of digits)
\) match a literal closing parenthesis
$ end of string

关于mysql - 如何将一列拆分为两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55555068/

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