SQL - 如何在不使用AND的情况下处理两个条件?

时间:2015-04-15 11:02:00

标签: mysql sql

我有这个问题:

String queryStr = 'SELECT COUNT(*) FROM tester x WHERE x.state !=:state'
setParameter("state", "state1");

我想要计算所有“state1”和“state2” 有没有办法做到而不是:

String queryStr = 'SELECT COUNT(*) FROM tester x WHERE (x.state !=:state1 AND x.state !=:state2)'; 
setParameter("state1", "state1");
setParameter("state2", "state2");

由于

1 个答案:

答案 0 :(得分:0)

不在

中使用
WHERE (x.state NOt IN (:state1,:state2))

那么你的整个查询将是

'SELECT COUNT(*) FROM tester x WHERE (x.state NOt IN (:state1,:state2))';

注意:上面的解决方案是根据您的查询,但根据您的文字[我想要计算所有“state1”和“state2”]

然后您的查询可以

'SELECT COUNT(*) FROM tester x WHERE x.state = :state1 AND x.state = :state2';