3

I am using MySQL 5.x, and am trying to come out with a SQL statement to select rows base on the following datasets

ID | Type           | Name
1  | Silver         | Customer A
2  | Golden         | Customer B
3  | Silver, Golden | Customer C 
4  | Bronze, Silver | Customer D 

I need to use regexp (Legacy system reasons) in the SQL statement, where I need to only select ID=1 and ID=4, which means I need "Silver", "Silver with Bronze" customer type, but not "Silver + Golden"

I am not very familiar with regular expressions, been trying with SQL like below:

SELECT DISTINCT `customer_type` FROM `customers` WHERE 
`customer_type` regexp 
"(Silver.*)(^[Golden].*)"

Where I need to have the regular expressions in one place like above, but not like below:

SELECT DISTINCT `customer_type` FROM `customers` WHERE 
`customer_type` regexp 
"(Silver.*)" 
AND NOT 
customer_type` regexp 
"(Golden.*)" 

Although LIKE will work, but I can't use it for special reasons.

SELECT DISTINCT `customer_type` FROM `customers` WHERE 
`customer_type` LIKE "%Silver%" 
AND NOT 
customer_type` LIKE "%Golden%"

I couldn't get the first SQL statement to work, and not sure even if that is possible.

11
  • 1
    If you are testing for the exact string 'Silver', why not just do where customer_type = 'Silver'? Commented Jan 20, 2015 at 10:35
  • 1
    try this , "(Silver)(?!.*Golden]).*" if the sql support negative lookahead. Commented Jan 20, 2015 at 10:36
  • shree.pat18 - edited my question to clarify further Commented Jan 20, 2015 at 10:40
  • Avinash Raj, thanks, but I get #1139 - Got error 'repetition-operator operand invalid' from regexp Commented Jan 20, 2015 at 10:41
  • 2
    Use LIKE instead of REGEXP when the values are known (and exact), i.e "where Type like '%Silver%' and type not like '%Golden%¨'" Commented Jan 20, 2015 at 10:43

1 Answer 1

1

Just try these one:

SELECT DISTINCT `id`, `customer_type` 
FROM `customers` 
WHERE `customer_type` regexp "^.*Silver$"

This matches "anything + Silver" or just Silver.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for answer, but it doesn't take into account if we have Silver +?
Ok, then I didn't got what you want. So let us work on a "ruleset", than I'll rewrite the query. So in my understanding there are the following rules: Allowed: "Silver", "Anything, Silver", "Silver, Anything". Not Allowed: "Silver, Gold". Are there other possible matches/rules?
Almost there with the rules, but instead of Not allowed Silver, Gold, which should be Not allowed Gold.
Basically following the rules like [ SELECT DISTINCT customer_type FROM customers WHERE customer_type LIKE "%Silver%" AND NOT customer_type` LIKE "%Golden%" ] but done in one regexp
Not sure if it can be done in MySQL because MySQL doesn't support negative lookahead in Regexp
|

Your Answer

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.