2011-02-24 37 views
4

假设我想搜索用户'Richard Best'。有可能比较全名是连接的名字和姓氏吗?我没有全名字段。将值与条款中的连接字段进行比较

select * from users where last_name + ' ' + first_name like '%richa%' 

我使用的是MySQL

+0

我们有了一个更好的办法来检查... – 2011-02-24 18:18:50

回答

9

这些相当于:

select * from users where concat(last_name,' ',first_name) like '%richa%' 

select * from users where concat_ws(' ',last_name,first_name) like '%richa%' 

这也可能工作:

select * from users where last_name like '%richa%' or first_name like '%richa%' 
+0

两个命题是不等价的: 随着“加斯奎特”,与“理查德·G”的搜索将不会与工作你的第二个解决方案,因为“理查德G”不匹配的名字或姓氏! – nlassaux 2014-10-26 15:17:46

+0

@ Nico401我不知道你在说什么。我坚持认为'concat(A,'',B)'concat_ws('',A,B)'是等价的。另一方面,如果你试图使用非concat语法匹配'like'%Richard G%'',那么这样做不会太好,duh。 – awm 2014-10-27 02:05:28

+0

对于concat和concat_ws等价,我很确定。但是连接和你的第三个命题(与OR)不是等价的。如果您编写姓氏和名字的一部分,concat解决方案将工作,尽管OR解决方案不会。 – nlassaux 2014-10-28 21:13:13

0
select * from users where (first_name + ' ' + last_name) like '%richa%' 
相关问题