我想搜索三个字段:id,名称和地址。我希望我的搜索有点聪明。

订户表

id         |     firstname     |      lastname     |     address
N29-0001   |     John          |      Doe          |     California
N29-0002   |     Jonathan      |      Morson       |     Miami


到目前为止我的查询

select subscriberid, CONCAT(firstname," ",lastname) as fullname, address, city from subscriber where id like "%N29 Joh%" or CONCAT(firstname," ",lastname) like "%N29 Joh%" or address like "%N29 Joh%" or city like "%N29 Joh%"


我想查询是否键入了N29 Joh,它将搜索John Doe。

最佳答案

您可能需要全文索引。但是,如果您想匹配任何单词并且不关心性能,则可以使用正则表达式:

select subscriberid, CONCAT(firstname, ' ', lastname) as fullname,
       address, city
from subscriber
where subscriberid regexp 'N29|Joh' or
      concat(firstname, ' ', lastname) like 'N29|Joh' or
      address like 'N29|Joh' or
      city like 'N29|Joh';

08-19 02:49