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

时间:2017-10-12 11:52:36

标签: mysql sql

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

Image From phpmyadmin with access sql query.

这是我的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

2 个答案:

答案 0 :(得分:9)

ref <  2 or user.usersecond=0 and paymentmethod = 1

Operator precedence。这被解释为:

(ref <  2) or (user.usersecond=0 and paymentmethod = 1)

由于相关记录与ref < 2匹配,因此会返回。

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

(ref <  2 or user.usersecond=0) and (paymentmethod = 1)

答案 1 :(得分:7)

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

id | comment_ids | updated_at
1   [12, 123, 11]   '2017-09-09'

大概你想要:

HAVING (ref <  2) or (user.usersecond = 0 and paymentmethod = 1)

如果您在条件中混合HAVING (ref < 2 or user.usersecond = 0) and paymentmethod = 1 and,请始终使用括号。