gpt4 book ai didi

mysql - 在 mySQL 中组合行中的字符串

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

我正在尝试对来自大型语料库(超过 100 万条记录)的字符串进行统计测试,我不确定如何最好地直接在 mySQL 中执行此操作而不是在 php 中解析它,这将花费(我至少基于根据我的技能)年龄。

表格:

ID              Words
---------- -------------
1 ham
2 cheese
3 lettuce
4 tomato
5 onion

我想要实现的期望输出是创建两个新列,其中包含每个字符串的二元组和三元组,如下所示:

ID              Words            Bigrams           Trigrams
---------- ------------- ------------- -------------
1 ham ham_cheese ham_cheese_lettuce
2 cheese cheese_lettuce cheese_lettuce_tomato
3 lettuce lettuce_tomato lettuce_tomato_onion
4 tomato tomato_onion ........
5 onion ......... ........

我想知道在 mySQL 中是否有办法做到这一点?

最佳答案

实现此目的的最佳方法是使用自连接。如果表名是 all_words,包含字段 idword:

SELECT
first.id,
first.word,
CONCAT(first.word, '_', second.word) AS bigram,
CONCAT(first.word, '_', second.word, '_', third.word) AS trigram
FROM
all_words first
LEFT JOIN
all_words second ON first.id + 1 = second.id
LEFT JOIN
all_words third ON first.id + 2 = third.id

使用 concat() 函数,您可以将不同表中的单词连接到一列中。如果您希望最后一行的二元组和三元组列为空(而不是只有前几个词),请使用

IF(second.word IS NOT NULL, CONCAT(first.word, '_', second.word), '') AS bigram

对于二元组和三元组相等的东西。

关于mysql - 在 mySQL 中组合行中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39064686/

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