user2321321
user2321321

Reputation: 13

Need regex expression in mysql select query

I am writing the mysql query where I need to compare data entered by user with the sku column in database tab

Query is like

SELECT * 
FROM  `is_product_info` 
WHERE REPLACE(  '-',  '', sku ) LIKE  '%ETI2006%'

so that if sku in database contains "-" in sku, I am replacing all hyphens with "" before comparing.

so whether sju no contains ETI2006 or ETI-2006, it will come in output

Upvotes: 1

Views: 60

Answers (4)

Bohemian
Bohemian

Reputation: 425033

Using replace() on every row is what's slowing it down.

All of these approaches should be faster - see which one works best for you:

Option 1 - use LIKE twice:

WHERE sku LIKE '%ETI-2006%'
   OR sku LIKE '%ETI2006%'

Option 2 - Use RLIKE once:

WHERE sku RLIKE 'ETI-?2006'

Upvotes: 0

user2321321
user2321321

Reputation: 13

I made the mistake in replace syntax, it works with the below one:

SELECT * FROM is_product_info WHERE REPLACE( sku , '-', '' ) LIKE '%ETI2006%'

Upvotes: 0

Munees Majid
Munees Majid

Reputation: 767

I think you may just like this

SELECT * FROM is_product_info WHERE REPLACE( sku , '-', '' ) LIKE '%ETI2006%'

Upvotes: 1

Can YILDIZ
Can YILDIZ

Reputation: 413

I didn't try on a running MySQL but this should work:

SELECT * 
FROM  `is_product_info` 
WHERE sku REGEXP REPLACE('ETI-2006','-','-?');

Upvotes: 0

Related Questions