regex - How to check exact integer value in string mysql -


i have stuck on problem column offer_description contains string amount(integer)....for example

buy above 200  buy above 900  buy above 300... 

a user can query this...offer 200...then running query this....

select * offertable offer_description '%200%';

or

select offer_description offertable offer_description rlike '200';

it works fine if have rs.200 offer but...in other scenario...where there different set of offers like....

buy above 200  buy above 1200 //new 1  buy above 2000 //new 1  buy above 900  buy above 300... 

so if execute above queries....it return 3 result...

buy above 200  buy above 1200 //new 1  buy above 2000 //new 1 

where want offer exact amount 200...

buy above 200....how can achieve this...please help

if pattern same i.e. amount @ end , separated space can use substring_index function

mysql> select substring_index('buy above 200',' ',-1) amount; +--------+ | amount | +--------+ | 200    | +--------+ 1 row in set (0.00 sec) 

so may query as

select * table  substring_index(col_name,' ',-1) = '200' 

also can use rlike matching

mysql> select 'buy above 200' rlike '[[:<:]]200[[:>:]]'; +-------------------------------------------+ | 'buy above 200' rlike '[[:<:]]200[[:>:]]' | +-------------------------------------------+ |                                         1 | +-------------------------------------------+ 1 row in set (0.00 sec)  mysql> select 'buy above 2000' rlike '[[:<:]]200[[:>:]]'; +--------------------------------------------+ | 'buy above 2000' rlike '[[:<:]]200[[:>:]]' | +--------------------------------------------+ |                                          0 | +--------------------------------------------+ 1 row in set (0.00 sec)  mysql> select 'buy 300 above' rlike '[[:<:]]200[[:>:]]'; +-------------------------------------------+ | 'buy 300 above' rlike '[[:<:]]200[[:>:]]' | +-------------------------------------------+ |                                         0 | +-------------------------------------------+ 1 row in set (0.00 sec)  mysql> select 'buy 200 above' rlike '[[:<:]]200[[:>:]]'; +-------------------------------------------+ | 'buy 200 above' rlike '[[:<:]]200[[:>:]]' | +-------------------------------------------+ |                                         1 | +-------------------------------------------+ 

so have data as

  • buy above 200
  • buy above 200 , more
  • 200 , more buy
  • buy above 2000
  • buy above 300

and select matching 200 , query be

select * table colname rlike '[[:<:]]200[[:>:]]'  

Comments