【问题标题】:MySQL RegEx search each columnMySQL RegEx 搜索每一列
【发布时间】:2012-03-22 20:45:31
【问题描述】:

我正在使用 Regex 搜索我的 MySQL 数据库,如下所示:

$search 看起来像这样 WORD1|WORD2|WORD3

$regexp = "REGEXP '[[:<:]][[:alpha:]]*($search)[[:alpha:]]*[[:>:]]'";

SELECT * FROM
    zitate
WHERE
    UPPER(`field1`) $regexp OR UPPER(`field2`) $regexp OR UPPER(`field3`)

现在,正如预期的那样,我得到了 $search 与 field1 或 field2 或 field3 匹配的每个条目

但我只想接收 WORD1、WORD2 和 WORD3 在每列中匹配的条目,而不是在整个表中。

我知道这是因为我的查询中出现了OR,但有什么比这样写更好:

WHERE
    UPPER(`field1`) $regexp AND UPPER(`field2`) $regexp
OR
    UPPER(`field2`) $regexp AND UPPER(`field3`) $regexp
OR
    UPPER(`field1`) $regexp AND UPPER(`field3`) $regexp
OR
    UPPER(`field1`) $regexp AND UPPER(`field2`) $regexp AND UPPER(`field3`) $regexp

或者我应该使用这个ANDOR 查询吗?

感谢任何提示和答案!

【问题讨论】:

  • 真的没人能给我建议吗?

标签: php mysql regex search


【解决方案1】:

想法:在 MySQL 中,逻辑值实际上是映射到 01,因此您可以简单地添加它们,而不是 OR

WHERE 1 < UPPER(`field1`) $regexp
        + UPPER(`field2`) $regexp
        + UPPER(`field3`) $regexp

或者,您应该了解 MySQL 的全文索引/搜索功能,或找到其他解决方案,例如 Fulltext Search with InnoDB

【讨论】:

    猜你喜欢
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    相关资源
    最近更新 更多