39

I'm using this query to select data:

mysql_query("SELECT * FROM products WHERE product_name LIKE '%".$search."%'");

The only problem is, that it sometimes selects more, than I would like.

For example, I would like to select product "BLA", but my query select product "BLABLA" as well. To be clear, if i wanted to select "Product 1", I don't want the query to select "Product 11".

Does anybody know how to manage that?

Thanks.

Reporter
  • 3,897
  • 5
  • 33
  • 47
Mike
  • 6,854
  • 17
  • 53
  • 68
  • well, that is the expected result you are using `LIKE`, try using `=` – Bastardo Apr 21 '11 at 11:03
  • Well, I didn't express myself clearly, the product_name column consists of many more words, than just the product_name itself. For example, there can be something like "Bla - 50g", "Bla - 10g", "Bla - 5g" and I want to select all "Bla's", but not "Blabla's". – Mike Apr 21 '11 at 11:16

12 Answers12

54

Do you just want to search on word boundaries? If so a crude version might be:

SELECT * FROM products WHERE product_name LIKE "% foo %";

Or you could be a bit cleverer and look for word boundaries with the following REGEXP

SELECT * FROM products WHERE product_name RLIKE "[[:<:]]foo[[:>:]]";
James C
  • 14,047
  • 1
  • 34
  • 43
  • 6
    I do not think the number one works well, think if 'foo ' value ! – Ata May 10 '14 at 17:16
  • 1
    The drawback of using RLIKE is: "The REGEXP and RLIKE operators work in byte-wise fashion, so they are not multibyte safe and may produce unexpected results with multibyte character sets. In addition, these operators compare characters by their byte values and accented characters may not compare as equal even if a given collation treats them as equal." – Sr. Libre Mar 16 '18 at 20:07
  • great dear, it helps me a lot thank you so much – Hadayat Niazi Dec 09 '21 at 16:13
14

Found this question on Google, so I figure that some people may still stumble upon this so here's my pretty inelegant attempt:

SELECT * FROM products
WHERE product_name LIKE 'BLA %' #First word proceeded by more words
OR WHERE product_name LIKE '% BLA' #Last word preceded by other words
OR WHERE product_name LIKE '% BLA %' #Word in between other words
OR WHERE product_name = 'BLA'; #Just the word itself

Not sure about the efficiency or if this covers all cases, so feel free to downvote if this is really inefficient or too inelegant.

PulpDood
  • 411
  • 5
  • 9
9

Try using regular expressions:

SELECT 
    *
FROM
    `products`
WHERE
    product_name regexp '(^|[[:space:]])BLA([[:space:]]|$)';
Faisal
  • 4,591
  • 3
  • 40
  • 49
8
SELECT  *
FROM    products
WHERE   product_name = 'BLA'

will select exact BLA

SELECT  *
FROM    products
WHERE   product_name LIKE 'BLA%'

will select BLADDER and BLACKBERRY but not REBLAND

To select BLA as the first word of the string, use:

SELECT  *
FROM    products
WHERE   product_name RLIKE '^Bla[[:>::]]'
        AND product_name LIKE 'Bla%'

The second condition may improve your query performance if you have an index on product_name.

Quassnoi
  • 413,100
  • 91
  • 616
  • 614
  • 1
    Well, I didn't express myself clearly, the product_name column consists of many more words, than just the product_name itself. For example, there can be something like "Bla - 50g", "Bla - 10g", "Bla - 5g" and I want to select all "Bla's", but not "Blabla's". – Mike Apr 21 '11 at 11:15
2

Remove LIKE keyword and use = for exact match

EDIT

do not forgot to escape user input using mysql_real_escape_string otherwise your query will fail if some one enter quotes inside the input box.

$search=mysql_real_escape_string($search);
mysql_query("SELECT * FROM products WHERE product_name='".$search."'");
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
1

Then don't use LIKE, but search for equality.

ie.

mysql_query("SELECT * FROM products WHERE product_name = '".$search."'");

BTW I hope you sanitize/escape $search before using it in a query.

wimvds
  • 12,790
  • 2
  • 41
  • 42
1

You can just cover all the possible options.

SELECT 
    * 
FROM 
    `products` 
WHERE
    `product_name` like 'BLA' -- Column cointains just that word
    OR `product_name` like 'BLA %' -- The word appears at the beginning
    OR `product_name` like '% BLA' -- The word appears at the end
    OR `product_name` like '% BLA %'; -- The word appears in the middle
Goldie
  • 1,570
  • 5
  • 21
  • 33
0

try to use regular expression in query

mysql_query("SELECT * FROM products WHERE product_name regexp '".$search."'");
Pankaj katiyar
  • 464
  • 10
  • 26
0

Use equals (=)?

mysql_query("SELECT * FROM products WHERE product_name = '".$search."'"); 

If you are looking to match EXACT words don't use LIKE.

EDIT: That clears things up a bit then. Just add a space after the search term. Or even add the hyphen (-) if that is always in the search term.

mysql_query("SELECT * FROM products WHERE product_name LIKE '".$search." -%'"); 
anothershrubery
  • 20,461
  • 14
  • 53
  • 98
  • Well, I didn't express myself clearly, the product_name column consists of many more words, than just the product_name itself. For example, there can be something like "Bla - 50g", "Bla - 10g", "Bla - 5g" and I want to select all "Bla's", but not "Blabla's". – Mike Apr 21 '11 at 11:17
  • Well, there can be absolutly anything, so that wouldn't work. =/ – Mike Apr 21 '11 at 11:23
  • What do you mean there could be absolutely anything?! If you are talking about the hyphen (-), just remove it and leave the space. That will only match words beginning with "Bla " and will therefore not select "Blabla" but will select "Bla -" or "Bla Bla". I only added the hyphen (-) as the examples you gave all had a hyphen. – anothershrubery Apr 21 '11 at 11:27
0

If you want to search exact word matching from MySql using LIKE then you use:

SELECT * FROM tableName WHERE columnName LIKE 'your_query' ; 
lczapski
  • 4,026
  • 3
  • 16
  • 32
0

In Laravel Eloquent you can do this like below.

Category::where('name', 'RLIKE ', "[[:<:]]"$words"[[:>:]]");

In raw query, you can search it like this.

SELECT * FROM categories WHERE name RLIKE "[[:<:]]categoryNameHere[[:>:]]";
Hadayat Niazi
  • 1,991
  • 3
  • 16
  • 28
-1

you can use select query like this ,i also use in cakePHP and it's helpful.

Select * from `users` where username COLLATE latin1_general_cs LIKE '%$email%'
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213