我有下面的SQL语句,但是我遇到一个问题,我还没有弄清楚如何正确地排序WHERE语句。

当我运行此语句时,它应该返回0,因为类别ID为2时没有问题,并且只要我取出or useranswers.level = 5,它就可以工作,因为还有其他类别ID为5级。

所以我的问题是我需要在哪里提出该声明?

SELECT COUNT(*) FROM useranswers INNER JOIN users on useranswers.userid = users.id
INNER JOIN kumiquestions on useranswers.questionid = kumiquestions.id
WHERE login = 'chris3spice'
and kumiquestions.categoryid = 2 and useranswers.level = 4 or useranswers.level = 5;

最佳答案

两种解决方案:

SELECT COUNT(*) FROM useranswers INNER JOIN users on useranswers.userid = users.id
INNER JOIN kumiquestions on useranswers.questionid = kumiquestions.id
WHERE login = 'chris3spice'
and kumiquestions.categoryid = 2 and (useranswers.level = 4 or useranswers.level = 5);


要么

SELECT COUNT(*) FROM useranswers INNER JOIN users on useranswers.userid = users.id
INNER JOIN kumiquestions on useranswers.questionid = kumiquestions.id
WHERE login = 'chris3spice'
and kumiquestions.categoryid = 2 and useranswers.level IN (4, 5);


两者在逻辑上等于同一件事。

10-04 11:14