mysql:最好的说法是 "the string up to the first instance of any of the following keywords"?

mysql: nicest way to say "the string up to the first instance of any of the following keywords"?

我可以想到在 mysql 中执行此操作的复杂且丑陋的方法,但我正在寻找一种不错的方法。假设我有一堆学校名称,比如

Meopham County Infant School
Speldhurst Nursery School
Rainbow Pre-School
The Annex School House
Fleet Learning Zone
Dartford Grammar School
Kiddliwinks
Hextable Kindergarten
The Rocking Horse Montessori Kinder
Little Angels Day Nursery

我有一个停用词列表:

["school", "primary", "nursery", "college", "junior", "church", "cofe", "community", "infant"]

我有一个 ruby 函数 "short_name" 其中 returns 学校名称最多 但不包括 ,任何的第一个实例停用词,所以我们得到

"Bower Grove School" => "Bower Grove"
"Fulston Manor School" => "Fulston Manor"
"St Johns Church Hall Play" => "St Johns"
"St Botolph's Church of England Voluntary Aided Primary School" => "St Botolph's"
"Fawkham House School" => "Fawkham House"
"Silverdale Day Nursery" => "Silverdale Day"
"Vigo Village School" => "Vigo Village"
"Sevenoaks Primary School" => "Sevenoaks"
"High Weald Academy" => "High Weald Academy"
"The Ebbsfleet Academy" => "The Ebbsfleet Academy"

没关系。我的问题是:在 mysql 中进行上述字符串处理的最简单方法是什么?

例如,如果我想通过这个 short_name 进行搜索,我想做类似

的事情
"select * from schools where <function(name)> = 'Bower Grove'"

最简单的方法是什么<function>?我认为使用正则表达式的 substring() 和 locate() 的某种组合将是可行的方法,但看起来我不能将正则表达式与定位一起使用。

我想正则表达式应该是

"school|primary|nursery|college|junior|church|cofe|community|infant"

谢谢,麦克斯

MySQL 确实支持正则表达式。不幸的是,它仅用于匹配。

这是一种方法:

select least(substring_index(schoolname, ' School', 1),
             substring_index(schoolname, ' Primary', 1),
             . . .
            )

这使用 substring_index() 在分隔符之前提取字符串的第一部分。如果分隔符不存在,您将获得整个字符串。 least() 函数将选择最短的字符串。

这假定该关键字前面有一个 space。毕竟,您可能不想完全消除像 "School for Little Angels".

这样的名称的所有内容