Skip to content
Advertisement

Using two sets of conditions in where clause

I’m trying to get the count of items in my table where it should satisfy these conditions

status = active
type = Pre-order
date = $date_input

OR

status = active
type = Both
date = $date_input

I’m trying to use this statement but I’m pretty sure it’s messed up.

SELECT COUNT(id) as count_date from date_restriction
where (date='$date_input' AND status='active' AND type='Pre-order') 
OR (date='$date_input' AND status='active' AND type='Both')

I also tried this to no avail

SELECT COUNT(id) as count_date from date_restriction
where date='$date_input' AND status='active' AND type='Pre-order' OR type='Both'

Advertisement

Answer

Your code should work. I would write this as:

select count(*) as count_date
from date_restriction
where date = ? AND status = 'active' AND
      type in ('Pre-order', 'Both');

Note: The ? is for a parameter so you are not munging the query string with input values.

If I had to guess why this isn’t working, I would speculate that one or both of the dates have a time component. You might try:

where date(date) = date(?) . . . 

to be sure you are matching on the date, regardless of time.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement