Cereais
Cereais

Reputation: 45

MySQL search column field string

I have a mysql column(name) with some text like this "firstname middlename surname lastname". How can i select that row with a query like

SELECT name FROM client WHERE name like %firstname lastname%

. The where clause comes from a single html input text.

Upvotes: 0

Views: 41

Answers (4)

O. Jones
O. Jones

Reputation: 108706

The LIKE operator takes string parameters: string LIKE string.

A string parameter can be a column name, in your case name.

Or it can be a string literal. String literals in MySQL are delimited by ' quotation marks. You didn't put them into your example, so it doesn't work. You want name like '%firstname lastname%' for your query to work.

Or it can be an expression. That's a story for another day.

Upvotes: 0

Dolotboy
Dolotboy

Reputation: 359

If you want it the way you asked, you could do it like this

SELECT name FROM client WHERE name LIKE 'John% %Doe'

So if in your table at the field "name" you have a value that is "John Fidzerald Mayhem Doe", it will be selected

But if I can advice you, create a column for each value you want, so a column for firstName which would not be nullable, a column for middlename which would be nullable, a column for surname which would be also nullable and a column for lastname which wouldn't be nullable

Upvotes: 1

nbk
nbk

Reputation: 49375

You can search by separating the words in two where argumants

 SELECT name FROM client WHERE name like '%firstname%' AND  name like '%lastname%'

Upvotes: 1

user1071914
user1071914

Reputation: 3403

Could you use

select name from client where name like '%firstname%' and name like '%lastname%'

Upvotes: 1

Related Questions