Sql查询没有得到正确的付款方式

kan*_*nnu 2 mysql sql

当我使用paymentmethod 1执行我的sql查询时,我遇到了问题.显示它还显示了paymentmethod 3.请检查我错在哪里.提前致谢

图像从phpmyadmin访问sql查询.

这是我的SQL查询: -

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
Run Code Online (Sandbox Code Playgroud)

Dav*_*vid 9

ref <  2 or user.usersecond=0 and paymentmethod = 1
Run Code Online (Sandbox Code Playgroud)

运算符优先级.这被解释为:

(ref <  2) or (user.usersecond=0 and paymentmethod = 1)
Run Code Online (Sandbox Code Playgroud)

由于有问题的记录匹配ref < 2,它们将被退回.

通过在括号中对表达式进行分组,明确定义逻辑的优先级:

(ref <  2 or user.usersecond=0) and (paymentmethod = 1)
Run Code Online (Sandbox Code Playgroud)


Gor*_*off 7

你需要括号.您的病情评估为:

HAVING (ref <  2) or (user.usersecond = 0 and paymentmethod = 1)
Run Code Online (Sandbox Code Playgroud)

大概你想要:

HAVING (ref <  2 or user.usersecond = 0) and paymentmethod = 1
Run Code Online (Sandbox Code Playgroud)

如果您在混合andor条件下,请始终使用括号.