Sanju Menon
Sanju Menon

Reputation: 685

Mutiple where condition in sql query

My db structure is as follows:

tender_id | slab_range | slab_checked | item_name
________________________________________________
1           2-3            Yes           Item1
1           2-7            No            Item2
1           2-9            Yes           Item 3
2                          No            Item4
2                          NULL          Item5

I need some multiple where condition to be checked in the query

First Condition is Show records only if slab_range value is there (Not Null), if slab_range value is there then it should show only records with Slab_checked=Yes

Next condition is if slab_range = NULL Then its should show all records with slab_checked = No or Null

This is the basic query which i need to change as per the above:

select tender_id, slab_range, slab_checked,item_name 
from quotation_items 
where tender_id='$tender_id' 
and (slab_range=!NULL or slab_checked='Yes' or slab_checked='') 
or  (slab_checked='No' or slab_checked='') 
order by item_name

Expected Result is

Expected Result

tender_id | slab_range | slab_checked | item_name
________________________________________________
1           2-3            Yes           Item1
1           2-9            Yes           Item 3
2                          No            Item4
2                          NULL          Item5

Upvotes: 1

Views: 61

Answers (1)

Ishamael
Ishamael

Reputation: 12795

select tender_id, slab_range, slab_checked,item_name 
from quotation_items 
where tender_id='$tender_id' 
and (
    (slab_range is not NULL and (slab_checked='Yes')) 
       or  
    (slab_range is NULL and (slab_checked='No' or slab_checked is NULL))
)
order by item_name

You might need to replace is NULL and is NOT NULL with <> '' and = '', depending on whether slab_range actually contains NULLs or empty strings (based on your desired output it seems to contain empty strings)

Upvotes: 1

Related Questions