Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%LIKE% retrieves eg. WOMEN DATA when calling MEN

Tags:

regex

php

laravel

  $itemList = DB::table('items')
                ->orderBy('id', 'desc')
                ->where('item_status', 2)
                ->where(function($query) use($queryArr)
                {
                    foreach($queryArr as $uID){
                        $query->whereRaw("tags LIKE '%$uID%'");
                    }
                })->paginate(21);

I have been facing this issue since a long time. Problem when you do a LIKE search is it grabs the data of WOMEN when it's just eg MEN

Mainly because MEN is inside Women

I also tried the following but failed(This sort of grab a word) men without women data

$query->where('tags', 'LIKE', '%'.$uID.'%');
SELECT 'a word a' REGEXP '[[:<:]]word[[:>:]]';

How do i use that Word boundary query in laravel query builder

Tried this and still failed $query->whereRaw("tags LIKE REGEXP '[[:<:]]Men[[:>:]]'");

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REGEXP '[[:<:]]Men[[:>:]]')' at line 1 (SQL: select count(*) as aggregate from items where item_status = ? and (tags LIKE REGEXP '[[:<:]]Men[[:>:]]')) (Bindings: array ( 0 => 2, ))

I also understand some asked why not just created a proper way of handling these item's category. Well i think for now using Full Text Search is fine for me at most when it come to scaling i will use elastic search. true?

UPDATE

Apologies for not giving an example of tags

Bag,Shoes,Men,Wedges

Bag,Shoes,Men

Men,Shoes,Bag

like image 961
CodeGuru Avatar asked Dec 26 '14 15:12

CodeGuru


1 Answers

If values are separated by commas, try to use following

WHERE `tags` REGEXP "(^|,)men(,|$)"

This will require to have comma or end of string around the word men.

like image 169
Alexey Matskoff Avatar answered Oct 26 '22 17:10

Alexey Matskoff