Reputation: 37
I got issue while i execute my sql query with paymentmethod 1 show it's show paymentmethod 3 also. Please Check where i am wrong. Thanks in advance
This is my sql query:-
SELECT (SELECT COUNT(u.`upperuserid`)
FROM user u
WHERE u.upperuserid = user.usernewid
) as ref,
usernewid,
user.paymentmethod,usersecond, mod_date
from user
HAVING ref < 2 or user.usersecond=0 and paymentmethod = 1
Upvotes: 2
Views: 196
Reputation: 218827
ref < 2 or user.usersecond=0 and paymentmethod = 1
Operator precedence. This is being interpreted as:
(ref < 2) or (user.usersecond=0 and paymentmethod = 1)
Since the records in question match ref < 2
, they are returned.
Explicitly define the precedence of your logic by grouping expressions in parentheses:
(ref < 2 or user.usersecond=0) and (paymentmethod = 1)
Upvotes: 9
Reputation: 1269743
You need parentheses. Your condition is evaluated as:
HAVING (ref < 2) or (user.usersecond = 0 and paymentmethod = 1)
Presumably you want:
HAVING (ref < 2 or user.usersecond = 0) and paymentmethod = 1
If you are mixing and
and or
in conditions, always use parentheses.
Upvotes: 7