Mysql 将值与where子句中连接的字段进行比较

Mysql 将值与where子句中连接的字段进行比较,mysql,sql,Mysql,Sql,假设我想搜索一个用户“Richard Best”。是否可以将全名与姓氏连接起来进行比较?我没有全名字段 select * from users where last_name + ' ' + first_name like '%richa%' 我使用的是Mysql,它们是等效的: select * from users where concat(last_name,' ',first_name) like '%richa%' select * from users where concat_

假设我想搜索一个用户“Richard Best”。是否可以将全名与姓氏连接起来进行比较?我没有全名字段

select * from users where last_name + ' ' + first_name like '%richa%'
我使用的是Mysql,它们是等效的:

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

看看这条线


在laravel Eloquent中,您可以使用
whereRaw(“concat(first_name',last_name)如%$search%”
必须有更好的方法来检查……这是否回答了您的问题?有两个命题是不等价的:对于“Richard Gasquet”,使用“Richard G”进行搜索不会与第二个解决方案一起工作,因为“Richard G”与firstname和lastname都不匹配@我不知道你在说什么。我坚持我的主张,
concat(A,,,B)
concat_ws('',A,B)
。另一方面,如果您试图使用非concat语法匹配
如“%Richard G%”
,那么这不会太好,duh。我同意concat和concat的等价性。但是连接和你的第三个命题(带或)是不等价的。如果您编写lastname和firstname的一部分,concat解决方案将起作用,尽管OR解决方案不起作用。第一节:“这些是等价的”。第二节:“这也可能有效”。从来没有人认为第二节等同于第一节。它在某些具体情况下有效,但我同意你的看法。以我的职位为准。
select * from users where (first_name + ' ' + last_name) like '%richa%'