0

I'm looking to search a large MYSQL table for a string and only return results that aren't between parentheses. If my table looks like this:

| ID |Data |
| 1 |Some search term here |
| 2 |Search term |
| 3 |Search term (search term) |
| 4 |(Search term) search term |
| 5 |Here's the (search term) |
| 6 |Never match this line |

And I want to select the rows where "search term" (case-insensitive) appears, but not in parentheses, how can I write the REGEXP to achieve that?

IDs 1,2,3,4 should return. IDs 5 & 6 shouldn't.

The search term may include letters, numbers and punctuation (though it shouldn't need to support parentheses as they'll never be nested).

asked Jan 20, 2014 at 16:36
0

1 Answer 1

1

I hate regex(or dont get them) so:

SELECT id,data FROM table 
WHERE CONCAT(REPLACE(SUBSTRING_INDEX(data,'(',1),'(',''),
REPLACE(SUBSTRING_INDEX(data,')',-1),')','')) 
LIKE '%search term%';

SQL Fiddle

answered Jan 20, 2014 at 20:23
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.