1

I'm building a query on this table

my_table
field1 | field2
1      | y
2      | y
3      | y
4      | n
1      | n
2      | n
3      | y
4      | y

I am looking for a rule (maybe IF) to select all the records excluding the ones where field1=1 AND field2=y

this record will not be selected:

field1 | field2
1      | y

while this is ok

field1 | field2
1      | N

EDIT:

I add this details: the query is longer:

SELECT * FROM my_table WHERE time>'time1' AND time<'time2' AND admin_area='0'

in this query i have to append a filter: if field2=Y exclude records where field1=1

0

3 Answers 3

3

How about:

SELECT * FROM my_table WHERE field1 <> 1 OR field2 <> 'y'
Sign up to request clarification or add additional context in comments.

3 Comments

what about if i have a long where and rules before?
i know the not operato but can you show me a query for this case?
You could wrap this part into a long WHERE clause, such as: WHERE blah blah blah AND (field1 <> 1 OR field2 <> 'y') AND blah blah blah...
2

Regarding:

in this query i have to append a filter: if field2=Y exclude records where field1=1

SELECT *
FROM my_table
WHERE time > 'time1'
AND   time < 'time2'
AND   admin_area = 0
AND ( (
    field2 = 'Y' AND field1 <> 1
) OR (
    field2 IS NULL OR field2 <> 'Y'
) )

Comments

0

Here is The MySQL SELECT syntax reference. You can also use != or <> for an inequality test.

1 Comment

SELECT * FROM my_table WHERE (field1, field2) <> (1, 'y');

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.