Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "and" and "or" in a "Where" clause

I have a query that is gathering information based on a set of conditions. Basically I want to know if a location has paid out more than $50 for the day OR the comment section has the word "filter" in it...

My query is:

SELECT        Store_Id, Paid_Out_Amount, Paid_Out_Comment, Paid_Out_Datetime, Update_UserName, Till_Number
FROM            Paid_Out_Tb
WHERE        (Store_Id = 1929) AND (Paid_Out_Datetime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)) AND (Paid_Out_Datetime < DATEADD(day, DATEDIFF(day, 0, 
                         GETDATE()), 0)) AND (Paid_Out_Amount > 50) OR
                         (Paid_Out_Comment LIKE N'%' + 'Filter' + '%')

The problem is It returns 460 results and should only return 2.

like image 942
Shmewnix Avatar asked Jul 23 '12 17:07

Shmewnix


People also ask

Can we use 2 conditions in WHERE clause?

Example - Two Conditions in the WHERE Clause (AND Condition)You can use the AND condition in the WHERE clause to specify more than 1 condition that must be met for the record to be selected.

Can I use and/or together in SQL?

Description. The SQL AND condition and OR condition can be combined to test for multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement. When combining these conditions, it is important to use parentheses so that the database knows what order to evaluate each condition.

Can we use or operator in WHERE clause?

The WHERE clause can be combined with AND , OR , and NOT operators.

Can I use two AND in WHERE clause in SQL?

You can specify multiple conditions in a single WHERE clause to, say, retrieve rows based on the values in multiple columns. You can use the AND and OR operators to combine two or more conditions into a compound condition.


2 Answers

AND takes precedence over OR. You need to group your conditions if you want to control the precedence. Try this:

SELECT        Store_Id, Paid_Out_Amount, Paid_Out_Comment, Paid_Out_Datetime, Update_UserName, Till_Number
FROM            Paid_Out_Tb
WHERE        (Store_Id = 1929) AND (Paid_Out_Datetime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)) AND (Paid_Out_Datetime < DATEADD(day, DATEDIFF(day, 0, 
                     GETDATE()), 0)) AND ( (Paid_Out_Amount > 50) OR
                     (Paid_Out_Comment LIKE N'%' + 'Filter' + '%') )
like image 69
ykaganovich Avatar answered Oct 28 '22 21:10

ykaganovich


You need to wrap your Paid_Out_Amoutn and Paid_Out_Comment criteria in a second set of parentheses:

SELECT Store_Id, Paid_Out_Amount, Paid_Out_Comment, Paid_Out_Datetime, 
    Update_UserName, Till_Number 
FROM Paid_Out_Tb 
WHERE (Store_Id = 1929) AND (Paid_Out_Datetime >= 
    DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)) AND 
    (Paid_Out_Datetime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)) AND 
    (
        (Paid_Out_Amount > 50) OR (Paid_Out_Comment LIKE N'%' + 'Filter' + '%')
    )
like image 34
LittleBobbyTables - Au Revoir Avatar answered Oct 28 '22 21:10

LittleBobbyTables - Au Revoir