1

I'm trying to select the name and lastname of all users which name+lastname starts with "John D", I mean it could select "John Doe", "John Deniro", "John Dalas".

but this select doesn't work:

SELECT name,lastname FROM users WHERE name+" "+lastname LIKE 'john D%';

So, how can I make that work?

Lukasz Szozda
  • 139,860
  • 19
  • 198
  • 228

3 Answers3

2

You could use CONCAT

SELECT name,lastname FROM users 
WHERE CONCAT(name , ' ', lastname) LIKE 'John D%';

or (better solution because conditions will be SARGable):

SELECT name,lastname FROM users 
WHERE name = 'John'
  and lastname LIKE 'D%'
Lukasz Szozda
  • 139,860
  • 19
  • 198
  • 228
0

You should split the parameters to get it working correctly.

SELECT `name`, `lastname`
FROM `users`
WHERE `name` LIKE "John%" AND `lastname` LIKE "D%"
Sergej
  • 1,778
  • 1
  • 14
  • 23
0

Or, if performance is vaguely important...

SELECT name
     , lastname 
  FROM users 
 WHERE name = 'John'
   AND lastname LIKE 'D%';
Strawberry
  • 33,338
  • 13
  • 38
  • 57