0

I have a table in my database that contains information like this:

activity: R1A1, R1A2, R1A2_2

My problem is where I'm trying to get all the activities that do not contain the second part (_2).

I'm trying this query: SELECT activity FROM table_name WHERE activity NOT LIKE '%_2' but I'm getting only the activity R1A1. This is because R1A2 contains a number two on its name. How can I solve it? What is the correct query to do that? I want to get all the activities without _2 or something like that on its name.

If I do in an inverse method (SELECT activity FROM table_name WHERE activity NOT LIKE 'R1%' I get the correct results.

How can I get what I need? Thanks for your answers!

Shadow
  • 33,525
  • 10
  • 51
  • 64
msabate
  • 335
  • 1
  • 2
  • 16

2 Answers2

1

in MYSQL, the underscore is a wildcard, much like the % except it matches one character. You can escape it with \ to get the literal.

Here is the correct query:

SELECT activity FROM table_name WHERE activity NOT LIKE '%\_2'
dustytrash
  • 1,568
  • 1
  • 10
  • 17
0

The underscore in LIKE is a wildcard that matches any character. You can escape it. Here is one method:

SELECT activity
FROM table_name
WHERE activity NOT LIKE '%$_2' ESCAPE '$';
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786