Skip to content
Advertisement

how to select multiple rows using where (two conditions) in sql?

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

Advertisement

Answer

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) 
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement