选择字段的不同子字符串值并计算该选择中字符的实例数

时间:2021-10-20 01:35:10

I'm trying to select distinct substring values of a field and count the number of instances of a char in that selection. I've found this wonderful post which answers half of it.

我正在尝试选择字段的不同子字符串值,并计算该选择中字符的实例数。我找到了这个很好的帖子,它回答了一半。

So, so far, i can count the instances of a char in my field, it works great. Now the even harder part, what if i select a piece of string using :

所以,到目前为止,我可以在我的领域中计算char的实例,它的效果很好。现在是更难的部分,如果我选择一段字符串使用:

SELECT DISTINCT SUBSTRING_INDEX(my_field, '-', -1) AS chunk

In this case i'm only selecting the last part of the string (everything after the last'-'). How can i apply this formula to chunk (trying to count the number of instances of '_' in the new string ? :

在这种情况下,我只选择字符串的最后一部分(在最后一个' - '之后的所有部分)。如何将此公式应用于块(尝试计算新字符串中'_'的实例数?):

(LENGTH(chunk) - LENGTH(REPLACE(chunk, '_', ''))) / LENGTH('_')

I know i shoud be using HAVING to make operation on chunk as it's not a real field, but how can i do something like :

我知道我应该使用HAVING来对块进行操作,因为它不是真正的领域,但我怎么能这样做:

SELECT DISTINCT SUBSTRING_INDEX(my_field, '-', -1) AS chunk, (LENGTH(chunk) - LENGTH(REPLACE(chunk, '_', ''))) / LENGTH('_') AS total FROM my_field HAVING total < 2 

The problem here is that i can't use 'chunk' in the last part since it's not a field..

这里的问题是我不能在最后一部分使用'chunk',因为它不是一个字段..

1 个解决方案

#1


3  

The problem here is that i can't use 'chunk' in the last part since it's not a field..

这里的问题是我不能在最后一部分使用'chunk',因为它不是一个字段..

Replace 'chunk' in the last part with

用最后一部分替换'chunk'

SUBSTRING_INDEX(my_field, '-', -1)

SUBSTRING_INDEX(my_field,' - ', - 1)

Don't know what's the problem?

不知道是什么问题?

#1


3  

The problem here is that i can't use 'chunk' in the last part since it's not a field..

这里的问题是我不能在最后一部分使用'chunk',因为它不是一个字段..

Replace 'chunk' in the last part with

用最后一部分替换'chunk'

SUBSTRING_INDEX(my_field, '-', -1)

SUBSTRING_INDEX(my_field,' - ', - 1)

Don't know what's the problem?

不知道是什么问题?