12

if column value is 2_apples or final_chapter how do i match a value that contains an underscore "_" ?

I've tried with:

SELECT DISTINCT brand FROM `items` WHERE brand like '%\\_'
SELECT DISTINCT brand FROM `items` WHERE brand like '_'
SELECT DISTINCT brand FROM `items` WHERE brand like '%_%'
SELECT DISTINCT brand FROM `items` WHERE brand like LIKE '\_' 

It either shows all results or none. Any suggestions?

RanaHaroon
  • 445
  • 6
  • 20

1 Answers1

20

Try

WHERE brand LIKE '%\_%'

The underscore is a wildcard for a single character whereas the % is a wildcard for multiple (or none) characters. Put a backslash in front to escape them.

If you don't like escaping things, because it's ugly or scary, then an alternative would be to use the REGEXP operator with underscore directly:

WHERE brand REGEXP '_'
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
fancyPants
  • 50,732
  • 33
  • 89
  • 96