0

When the condition is based on one column, I can use either IN, AND, or OR, but if the condition is based on two columns, how to write the query? For example, I need to set up the value of column 1 to NULL in the following rows: column 2 = 'red' AND column 3 =' Monday', column 2 = 'yellow' and column 3 = 'Tuesday'. 'red' or 'yellow' is not unique in column 2, the same for 'Monday' and 'Tuesday'. I have quite of few of rows need to be updated. How can I do this in one query?

column1   column2    column3    
1         red        Monday    
2         red        Tuesday    
3         yellow     Monday    
4         yellow     Tuesday    
5         green      Wednesday

The results look like this:

column1   column2    column3    
NULL      red        Monday    
2         red        Tuesday    
3         yellow     Monday    
NULL      yellow     Tuesday    
5         green      Wednesday

2 Answers 2

2

WHERE clauses boil down to a simple true/false value: true - the row matches, so do something. false - no match, don't do anything.

If you have multiple independent conditions, then

WHERE (condition1) OR (condition2) OR ...

And each of those sub-conditions can be as arbitrarily complicated/simple as you need, including having their own in clauses:

WHERE (foo IN ('a','b','c')) OR (bar IN ('p','q', 'r')) OR (baz = 42) 
Sign up to request clarification or add additional context in comments.

Comments

1
UPDATE yourTable SET column1 = NULL WHERE (column2 = 'red' AND column3 = 'Monday') OR (column2 = 'yellow' AND column3 = 'Tuesday')

Comments

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.