繁体   English   中英

MySQL:在分割名称表中搜索全名

[英]Mysql: search fullname in a splitted name table

我正在寻找一种可以在拆分的柱表中查找全名的方法

我尝试过:例如使用“迈克尔·彼得·约翰逊”

select firstName,middleName,lastName 
from staff 
where concat(firstName, ' ', middlename, ' ', lastname) Like "%michael peter johnson%"

可以,但是如果名称为"michael johnson"则无法使用,这会导致创建的concat名称: "michael (double space)johnson"因此concat将创建两个空格,并且该空格不匹配。

有谁知道替代方法来解决此问题?

编辑:想法是字符串“ michael peter johnson”是用户输入字段。 因此无法将其分成3个单独的字符串a,这不是此搜索栏的想法

Edit2:我还注意到,如果Middlename为“ NULL”,并且concat(firstName,'',middlename,'',lastname)与NU ::的结果为NULL,则它将永远找不到。

有什么解决办法吗?

玛蒂(Thx Matthy)

您可以这样做:

select firstName, middleName, lastName 
from staff 
where concat(firstName, ' ', middlename, ' ', lastname) Like "%michael%peter%johnson%"

但我认为您想要:

select firstName, middleName, lastName 
from staff 
where firstName like '%michael%' and
      middleName like '%peter%' and
      lastName like '%johnson%';

或者因为您的数据库值似乎有空格,也许只是:

select firstName, middleName, lastName 
from staff 
where trim(firstName) = 'Michael' and
      trim(middleName) = 'Peter' and
      trim (lastName) = 'Johnson';

编辑:

或者,您可以执行以下操作:

where concat(trim(firstName), ' ', trim(middlename), ' ', trim(lastname)) Like concat('%', 'michael peter johnson', '%')

Replace是另一种选择:

select firstName,middleName,lastName 
from staff 
where replace(concat(firstName, ' ', middlename, ' ', lastname), '  ', ' ') Like "%michael peter johnson%"

如果你和迈克尔·约翰逊一起讲课。 做这个

     select name, midle, last 
     from staff
     where concat(name, ' ', midle, ' ', last) Like "%michael%johnson%"

这里演示

编辑:

 select firstName,middleName,lastName 
 from table1
 where (firstName Like "%michael%" AND lastName  LIKE "%johnson%" )

可能不是最有效的解决方案,但应该可以满足您的要求:

select firstName, midlename, lastname 
from staff
where replace(concat(ifnull(firstName, ''), ifnull(middlename, ''), ifnull(lastname, '')), ' ', '') = replace('michael peter johnson', ' ', '');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM