简体   繁体   中英

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 . 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.

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...

Appreciate your help!

This should work (see SQL Fiddle demo ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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