简体   繁体   English

在WHERE SQL中结合使用CONCAT和RTRIM

[英]Combine CONCAT and RTRIM in WHERE SQL

I'm in a bit of trouble at the moment and have no idea how to fix this. 目前,我有点麻烦,不知道如何解决。

Some background info first: 首先了解一些背景信息:

I have a table with 71k records, with columns firstname and lastname . 我有一个包含71k条记录的表,其中列firstnamelastname A client wants to be able to search with firstname and lastname combined. 客户希望能够将姓氏和名字组合在一起进行搜索。

I used this query and it works fine: 我使用了此查询,它工作正常:

select * 
from `subscribers` 
where `subscribers`.`deleted_at` is null 
  and concat   (firstname," ",lastname) = 'firstname lastname'

except for the following problem: 除了以下问题:

some records have a trailing whitespace behind their firstname. 有些记录的名字后面有空格。 which makes the above query not work, unless I add a second space in between firstname and lastname. 除非我在名字和姓氏之间添加第二个空格,否则上述查询将无法工作。

I know I have to use RTRIM on my firstname , which I tried, but doesn't seem to work. 我知道我必须在我的firstname上使用RTRIM ,我尝试过但似乎没有用。

What am I doing wrong? 我究竟做错了什么? How can I fix this? 我怎样才能解决这个问题? I rather don't edit 71k records so they don't have a trailing space behind their names anymore... 我宁愿不编辑71k记录,因此它们的名称后面不再有尾随空格...

Appreciate your help! 感谢您的帮助!

This should work (see SQL Fiddle demo ): 这应该可以工作(请参阅SQL Fiddle演示 ):

select * from `subscribers` 
where `subscribers`.`deleted_at` is null 
  and concat (trim(firstname)," ",trim(lastname)) LIKE 'firstname lastname';

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

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