gpt4 book ai didi

MySQL:从字符串(或varchar字段)中提取第一个、第二个和第三个单词

转载 作者:行者123 更新时间:2023-11-29 15:47:36 25 4
gpt4 key购买 nike

如何提取MySQL中varchar字段中的第一个第二个单词?

最佳答案

只需使用 SUBSTRING_INDEX ( official MySQL documentation , official MariaDb Documentation )

SELECT Substring_index(`sentence`, ' ', 1)                           AS first_word, 
Substring_index(Substring_index(`sentence`, ' ', 2), ' ', -1) AS second_word,
Substring_index(Substring_index(`sentence`, ' ', 3), ' ', -1) AS third_word
FROM `test`

https://www.db-fiddle.com/f/iSGActpsMNzHze13W2QA3q/1

还可以使用此解决方案使其更加通用:

SET @N = 3; --3rd word
SET @delimiter = ' ';
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(`sentence`, @delimiter, @N), @delimiter, -1)
FROM
`test`;

credits to this guy

关于MySQL:从字符串(或varchar字段)中提取第一个、第二个和第三个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56950615/

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