Encry
Encry

Reputation: 13

Why is my OR operator not working in my WHERE clause?

I need to write a where statement that only returns names starting with A, B, C, or E. I have a WHERE clause with a less that D condition, and that seems to work fine. But my Equals E condition doesn't appear to be working.

SELECT vendor_name,
    CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS full_name
FROM vendors
WHERE vendor_contact_last_name < 'D' 
    OR vendor_contact_last_name = 'E'
ORDER BY vendor_contact_last_name, vendor_contact_first_name;

It is only returning names that start with A, B, and C.

Upvotes: 1

Views: 58

Answers (1)

Sebastian Brosch
Sebastian Brosch

Reputation: 43574

You can use the following using LEFT:

SELECT vendor_name,
    CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS full_name
FROM vendors
WHERE LEFT(vendor_contact_last_name, 1) IN ('A', 'B', 'C', 'E')
ORDER BY vendor_contact_last_name, vendor_contact_first_name;

... or using RLIKE:

SELECT vendor_name,
    CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS full_name
FROM vendors
WHERE vendor_contact_last_name RLIKE '^[A-CE]'
ORDER BY vendor_contact_last_name, vendor_contact_first_name;

demo on dbfiddle.uk

Upvotes: 1

Related Questions