i need to generate a SQL Query using logic implemented in java, After WHERE conditions there can be multiple comparitions using AND OR NOT, eg:
x
WHERE column1 = 'xyz'
and column2 = 2 or column1 = 'abc'
and column2 = 1 and column4 IN(SUBQUERY1 condition) or column4 IN(SUBQUERY1 condition)
This can be re-write as
WHERE column1 IN('xyz','abc') and column2 .. ??
and column4 IN(SUBQUERY1 IN OR NOT OR AND)
tell me the logic how to group them properly to form a query.
Thanks Prasanth
Advertisement
Answer
You should wrap your condition by ()
to make each condition is properly.
WHERE column1 = 'xyz'
and (column2 = 2 or column1 = 'abc')
and ((column2 = 1 and column4 IN(SUBQUERY1 condition)) or column4 IN(SUBQUERY1 condition))