mysqlsqlsubqueryconditional-statementsinner-query

output with condition based sub query in mysql


Below is my query:

select c1, c2, c3, c4, c5 from table1 where c1 =0 //Condition 1
AND c2="abc" //Condition 2
AND c3="xxx" //Condition 3
AND c4 = 5 //Condition 4

Here as we know that condition 2 will be applied on result data given by condition 1, condition 3 will be applied on on result data given by condition 2 and 1 and similarly condition 4 will be applied...

I want query which will execute further if any condition in between fails.

Means if condition 3 will give no result (or null result set) then my condition 4 will be applied on null result set so defiantly final output will be null, but I want if condition 3 will give no result (or null result set) then condition 4 "Should be Applied" to result set return by condition 2 and 1.

Here I can not change sequence of conditions due to performance issue.

Please guide me with query.


Solution

  • I got one simple solution for this and it is working in my scenario. Also I am able to reduce time for query execution on my big data. Defiantly two queries will be executed but i find this proper solution.

    I used simple Union query.

    select c1, c2, c3, c4, c5 from table1 where c1 =0 //Condition 1
    AND c2="abc" //Condition 2
    AND c3="xxx" //Condition 3
    AND c4 = 5 //Condition 4
    UNION
    select c1, c2, c3, c4, c5 from table1 where c1 =0 //Condition 1
    AND c2="abc" //Condition 2
    AND c4 = 5 //Condition 4

    If someone else has more accurate and proper solution. Please post it.