我正在使用 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
或者我应该使用这个AND
, OR
查询吗?
感谢任何提示和答案!
最佳答案
想法:在 MySQL 中,逻辑值实际上映射到 0
和 1
,因此您可以简单地将它们相加,而不是对它们进行 OR
喜欢:
WHERE 1 < UPPER(`field1`) $regexp
+ UPPER(`field2`) $regexp
+ UPPER(`field3`) $regexp
或者,您应该了解 MySQL 的全文索引/搜索功能,或者寻找其他解决方案,例如 Fulltext Search with InnoDB .
关于php - MySQL RegEx 搜索每一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9565632/