0

I've a field "locations" containing values in format 1,4,7,8 etc.

I want to exclude rows that contain a specific value. eg: $location = 4;

I tried: SELECT * FROM users WHERE FIND_IN_SET($location, locations) = 0

Also tried some combinations with IN .. !IN but again no results. Any suggestion?

Thank you

2
  • SELECT * FROM users WHERE location<>'$location' ? You want to exclude data based from a submitted form, right? Commented Oct 16, 2014 at 5:57
  • 2
    uhmm, it's working on fiddle: sqlfiddle.com/#!2/1dab9/1 Commented Oct 16, 2014 at 5:58

2 Answers 2

0

As pointed by John Woo your query must work. If not then may be data in the locations contains values with spaces before commas, for example: 1,4 ,7,8. In this case FIND_IN_SET isn't working as expected. So you can try:

SELECT * FROM users WHERE NOT(locations REGEXP '[[:<:]]$location[[:>:]]')

[[:<:]], [[:>:]] stand for word boundaries, so [[:<:]]4[[:>:]] will match 4 in list, but not 14.

Test and see difference on SQL Fiddle

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

5 Comments

1.- Confirmed that there is no space between locations. 2.- Your example bring parse error: syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
I replace the variable $location with the actual value and it works, but works on the opposite way. Maybe if I remove NOT?
Did you enclose this query with double quotes " in php script?
This is my query right now: "SELECT * FROM users WHERE NOT(locations REGEXP '[[:<:]]".$location."[[:>:]]') AND active=1 AND hidden=0" As I wrote before works, but opposite way. In cases that normal should not shows rows, it shows them, and in cases that it must shows data, it does not shows.
Ok. I removed NOT from query and now works as expecetd. Maybe I was not clear by saying that "EXCLUDE".
0

SELECT * FROM users WHERE FIND_IN_SET($location, locations) != 0

3 Comments

I would think it should be $location='4' as FIND_IN_SET is a string function. Hard to tell without seeing the field types. dev.mysql.com/doc/refman/5.0/en/… refers.
Field locations is a VARCHAR field type, so it's ok. I had a typo before. I want to EXCLUDE rows. I tried your suggestion but unfortunately does not works. Also tried !FIND_IN_SET($location, locations) > 0 . Nothing. What must be first parameter? The variable or the field name?
Take a look at John Woo's answer above. Follow the link to his jfiddle. It is working, so that should help you?, so try "SELECT * FROM users WHERE FIND_IN_SET('$location', locations) = 0"

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.