gpt4 book ai didi

MySQL,以空格分隔排名关键字

转载 作者:行者123 更新时间:2023-11-30 22:44:30 24 4
gpt4 key购买 nike

我有一个只有一个列的表格,包含自由文本,每个关键字用空格分隔

TEXT
apple orange dog
apple banna cat

我想要一个结果......

apple 2
orange 1
dog 1
banna 1
cat 1

仅使用 SQL 是否可行?

最佳答案

试试这个

创建函数

Create FUNCTION [dbo].[fn_Split](@text varchar(8000), @delimiter varchar(20))
RETURNS @Strings TABLE
(
position int IDENTITY PRIMARY KEY,
value varchar(8000)
)
AS
BEGIN

DECLARE @index int
SET @index = -1

WHILE (LEN(@text) > 0)
BEGIN
SET @index = CHARINDEX(@delimiter , @text)
IF (@index = 0) AND (LEN(@text) > 0)
BEGIN
INSERT INTO @Strings VALUES (@text)
BREAK
END
IF (@index > 1)
BEGIN
INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))
SET @text = RIGHT(@text, (LEN(@text) - @index))
END
ELSE
SET @text = RIGHT(@text, (LEN(@text) - @index))
END
RETURN
END

查询

 declare @ColumnNameList VARCHAR(MAX)
select @ColumnNameList = COALESCE(@ColumnNameList +' ' ,'') +column from table
select @ColumnNameList
select value,count(value) as count from (select value from fn_split((select @ColumnNameList),' '))
Space_tbl group by value

关于MySQL,以空格分隔排名关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30022295/

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