MySQL在任何地方找到包含所有字母的单词

标签 mysql regex

使用以下查询

SELECT * FROM words WHERE word REGEXP '(ord)'
  • 这将返回诸如 fordlordword 之类的词

如何包含这些字母出现但顺序不同的单词?

  • 例如adoredoorrandom

编辑:

得到这个工作。

SELECT word, (
    IF(LOCATE('o', word) > 0, 1, 0) + 
    IF(LOCATE('r', word) > 0, 1, 0) +
    IF(LOCATE('z', word) > 0, 1, 0) +
    IF(LOCATE('a', word) > 0, 1, 0) +
    IF(LOCATE('d', word) > 0, 1, 0)) AS chars_present
from words
HAVING chars_present = 5

现在我如何查询两次包含字母 r 的单词?

最佳答案

在 mysql 中做一个正则表达式要求所有三个字符出现在任何位置是很丑陋的,你可能会更好:

SELECT (
    IF(LOCATE('o', words) > 0, 1, 0) + 
    IF(LOCATE('r', words) > 0, 1, 0) +
    IF(LOCATE('d', words) > 0, 1, 0)) AS chars_present
...
HAVING chars_present = 3

关于MySQL在任何地方找到包含所有字母的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29776870/

相关文章:

regex - 检查正则表达式模式匹配是否失败

php - Mysql 没有按要求的顺序执行 select 语句

php - 从特定日期开始按 7 天间隔对行进行分组

MySql - 错过的事件时间表

php - 如何将这三个 php 提要合并为一个提要

python - Python 中的正则表达式替换

regex - 地址字段验证的正则表达式

java - 如何检查字符串是否有重复模式?

regex - Perl:找不到文件时抑制反引号的输出

php - sudo mysqld_safe --skip-grant-tables & ,/var/lib/mysql 重置 root 密码时出错