Skip to content
Advertisement

SQL Where data is equals to 2 different values

I have the following query:

SELECT b.id, b.created_at, b.updated_at, b.reservation_code, b.reservation_type, b.reservation_date,
                   b.reservation_time, b.party_size, b.customer_code, b.deposit_total,
                   b.payment_code, b.voucher_code, b.reservation_processed, b.payment_status,
                   b.receipt_mailed, b.reservation_status, b.special_requirements, b.email_csv, b.occasion_type, p.name, p.slug
            FROM bookings b
            JOIN premises p ON p.code = b.premises_code WHERE b.premises_code = ? AND b.reservation_processed != 'no-show' OR b.reservation_processed != 'removed'

I want to return rows where reservation is anything EXCEPT ‘no-show’ or ‘removed’

Right now it’s returning rows still that are ‘removed’

I ran the command directly in sql and the same problem 🙁

Advertisement

Answer

Use NOT IN:

WHERE b.premises_code = ? AND
      b.reservation_processed NOT IN ('no-show', 'removed')

You seem unfamiliar with the rules of boolean logic. Your logic is interpreted as:

WHERE (b.premises_code = ? AND b.reservation_processed <> 'no-show') OR 
      (b.reservation_processed <> 'removed')

But you actually intend:

WHERE b.premises_code = ? AND
      b.reservation_processed <> 'no-show' AND
      b.reservation_processed <> 'removed'
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement