Mellon
Mellon

Reputation: 38882

"Where" statement : contains a certain substring

I am using MySQL.

I have a car table in my database, and there is a name column in that table.

Suppose the name column of the table contain values:

 +----------+
 |   name   |
 +----------+
 | AAA BB   |
  ----------
 | CC D BB  |
  ----------
 | OO kk BB |
  ----------
 | PP B CC  |
  ----------

I would like to search the table where name column value contains "BB" , What is the SQL command to achieve this ?

I tried :

SELECT * FROM car WHERE name LIKE "BB";

But it does not work.

P.S.

The values in name column are random strings.

Please do not ask me to use IN (...) , because the values in that column is unpredictable.

---------Please close this question--------------

Sorry, I think I asked the question wrongly. Actually I am asking for a word match not a substring.

Upvotes: 14

Views: 114060

Answers (1)

Matteo Alessani
Matteo Alessani

Reputation: 10422

You need to include the % operator to select records that contain "BB" in the name field, not only the exact value.

SELECT * FROM car WHERE name LIKE '%BB%';

Upvotes: 45

Related Questions