0

这是我的查询:

SELECT COUNT(*) FROM jos_mls AS mls 
    INNER JOIN jos_activeagents AS active ON active.AGENTUID = mls.MSTLISTBRD
    AND mls.MSTACTSTA = 'Active' OR mls.MSTACTSTA = 'Keep Showing';

表格很大,所以很难显示表格结构。

当我在没有 的情况下运行计数时AND mls.MSTACTSTA = 'Active' OR mls.MSTACTSTA = 'Keep Showing',我得到了预期的结果(大约 500)。但是当我想用这些参数过滤时,它会返回 25000+ 个结果,而它应该低于 500。

我认为这是一个语法问题,但查找类似的问题,我无法提出解决方案。

4

3 回答 3

0

使用WHERE子句

SELECT COUNT(*) FROM jos_mls AS mls 
    INNER JOIN jos_activeagents AS active ON active.AGENTUID = mls.MSTLISTBRD
    WHERE mls.MSTACTSTA = 'Active' OR mls.MSTACTSTA = 'Keep Showing';
于 2013-04-03T14:51:32.133 回答
0
SELECT COUNT(*) 
  FROM jos_mls mls 
  JOIN jos_activeagents active 
    ON active.AGENTUID = mls.MSTLISTBRD
   AND (mls.MSTACTSTA = 'Active' OR mls.MSTACTSTA = 'Keep Showing');

或者

SELECT COUNT(*) 
  FROM jos_mls mls 
  JOIN jos_activeagents active 
    ON active.AGENTUID = mls.MSTLISTBRD
   AND mls.MSTACTSTA IN('Active','Keep Showing');
于 2013-04-03T14:55:11.627 回答
0

AND具有优先权,OR因此您应该在条件的第二部分周围使用括号来获得所需的输出:

SELECT COUNT(*) FROM jos_mls AS mls 
    INNER JOIN jos_activeagents AS active ON active.AGENTUID = mls.MSTLISTBRD
    AND (mls.MSTACTSTA = 'Active' OR mls.MSTACTSTA = 'Keep Showing');

您获得的行数比预期多得多的原因是您运行的实际查询与此相同:

SELECT COUNT(*) FROM jos_mls AS mls 
    INNER JOIN jos_activeagents AS active ON (active.AGENTUID = mls.MSTLISTBRD
    AND mls.MSTACTSTA = 'Active') OR mls.MSTACTSTA = 'Keep Showing';
于 2013-04-03T14:57:26.457 回答