Pages

Monday, May 18, 2015

How to add additional conditions within a where condition depending on the value got in a where condition

Hi folks,

                            I have recently got a task to add additional condition to a particular query. This additional condition should be validate if a particular condition got true. Let's illustrate this as below,

SELECT col1, col2, col3...
FROM table1
WHERE col3 = 'val 1' OR col3 = 'val 2'
AND ........................

Lets say we have to add and additional condition if col3 = val 1. If col3 = val 2 then we dont have to check for this additional condition (lets say for col4). 
This is how we can simply do this.

SELECT col1, col2, col3...
FROM table1
WHERE ((col3 = 'val 1' AND col4 = 'val 3') OR col3 = 'val 2')
AND ........................ 

In the above query, it will check for col4 = 'val 3' when col3 = val 1. If col3 = 'val 2' the additional check will be neglected and the rest is same.