0

我有一个以Customers列命名的表FirstName, LastName, Email

假设我有客户:John | 威廉姆斯 | johnW1234@gmail.com

现在我必须如何创建我的查询,以便如果我搜索:

"williams"       =>  match
"will john"      =>  Match
"will john 55"   =>  NO match
"will 1234"      =>  match

我的查询现在看起来像:

SELECT * FROM `Customers` WHERE `FirstName` LIKE _search_ OR `LastName` LIKE _search__

但是,如果有人在哪里寻找“will john”,那么我的查询将不返回任何匹配项

4

4 回答 4

1

我认为这有效:

select * from Customers
where (_search_ regexp '^[^ ]+ [^ ]+$' or _search_ regexp '^[^ ]+$')
and (LastName like concat(substring_index(_search_, ' ',  1), '%'))
or FirstName like concat(substring_index(_search_, ' ',  -1), '%')));
于 2013-10-28T16:08:44.177 回答
1

好像你想做这样的事情:

select * from Customers where 
(FirstName like concat('%','john', '%') and LastName like concat('%','smith', '%'))
or
(LastName like concat('%','john', '%') and FirstName like concat('%','smith', '%'))

部分:johnsmith(在查询中)是搜索词的不同部分,由空格分解并修改为小写(您可以在代码或数据库中进行)。

链接到小提琴

于 2013-10-28T16:09:53.713 回答
0

动态sql可以帮到你

 EXECUTE 'SELECT * FROM Customers WHERE FirstName LIKE ' ||
 _search_ || ' OR LastName LIKE ' || _search__ || ';';

“_ 搜索 _” 应转换为文本(明确或不明确)。

当然,报价等待您的关注。

于 2013-10-28T16:01:51.387 回答
0

粗鲁...

SET @string = 'John|Williams|johnW1234@gmail.com';

SELECT IF(@string LIKE "%will%",IF(@string LIKE "%john%",IF(@string LIKE "%55%",1,0),0),0)x;
+---+
| x |
+---+
| 0 |
+---+

SELECT IF(@string LIKE "%will%",IF(@string LIKE "%john%",IF(@string LIKE "%12%",1,0),0),0)x;
+---+
| x |
+---+
| 1 |
+---+
于 2013-10-28T16:18:03.913 回答