在mysql中的CONCAT中添加通配符

时间:2016-12-06 02:32:51

标签: mysql wildcard concat

我想搜索三个字段:id,name和address。我希望我的搜索有点聪明。

订阅者表

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。

1 个答案:

答案 0 :(得分:1)

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

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';