0

我设法在一些帮助下想出了这个,但是由于某种原因它不能正常工作。最后的整行与类别似乎很随机,我无法弄清楚它在做什么。

使用或持有似乎和这个月一样工作正常,我相信如果所有项目都在一般类别中,但它在其他任何地方都可以正常工作并且它变得很奇怪。

如果有人能看到问题,我将不胜感激。

WHERE (Status = 'Used' OR Status = 'Holding') AND MONTH( Display_Date )=4 AND Category = 'General' OR Category = 'mountains' OR Category = 'rivers' OR Category = 'sky' OR Category = 'crass' OR Category = 'sea' OR Category = 'flowers'

4

3 回答 3

0

很可能它与括号有关。我猜你想放一些类似的东西.-

WHERE (Status = 'Used' OR Status = 'Holding') AND 
      MONTH(Display_Date) = 4 AND 
      (Category = 'General' OR Category = 'mountains' OR Category = 'rivers' OR Category = 'sky' OR Category = 'crass' OR Category = 'sea' OR Category = 'flowers')

请记住,AND它的优先级高于OR,因此如果没有括号,您的原始条件将true与任何 OR 语句一样长true,而不管先前的AND条件。

于 2013-04-16T18:11:27.713 回答
0

我假设您说任何与您指定的任何类别值匹配的结果(以及匹配对 Status 和 Display_Date 的检查)都应该返回。如果是这种情况,您需要将所有针对 Category 的检查括在括号中:

WHERE (Status = 'Used' OR Status = 'Holding') AND MONTH( Display_Date )=4 AND     (Category = 'General' OR Category = 'mountains' OR Category = 'rivers' OR Category = 'sky' OR Category = 'crass' OR Category = 'sea' OR Category = 'flowers')
于 2013-04-16T18:11:54.363 回答
0

AND优先于OR,因此您现在的条件是:

WHERE ((Status = 'Used' OR Status = 'Holding') AND MONTH( Display_Date ) = 4 AND Category = 'General') OR Category = 'mountains' OR Category = 'rivers' OR Category = 'sky' OR Category = 'crass' OR Category = 'sea' OR Category = 'flowers'

我假设你想要这个:

WHERE  status IN ('used', 'holding')
       AND MONTH(Display_Date) = 4
       AND category IN ('general', 'mountains', 'rivers', 'sky', 'grass', 'sea')
于 2013-04-16T18:12:21.910 回答