0

如果我想要下表中的日期/记录使得年份大于或等于 2012 年且月份大于九月,我应该如何形成查询。

这个查询不起作用它带来了 Months 2012 (7,8, 9,10,11,12) , 2013(1 upto 12) 这是不正确的,因为我想看到 2012(9,10,11,12) 2013( 1 到 12)。包括 2012 年的第 7 个月和第 8 个月

select * from ConfigurationDate
where Year >= 2012 OR ( Year = 2012 AND Month >= 9 )
Order By Year,Month ASC

表架构

DateId INT Auto Inc

Year INT

Month INT

虚拟数据

DateId           Year        Month
1                2012          7

2                2012          8

3                2012          9

4                2012          10

5                2012          11

6                2012          12

7                2013          1

8                2013          2

9                2013          3

10               2013          4

11               2013          5

12               2013          6

13               2013          7

14               2013          8

15               2013          9

16               2013          10
4

1 回答 1

0

实际上考虑一下,我不需要在 First where 条件中再次包含 2012 年的日期,因为它已被第二个条件覆盖。所以答案在下面

select * from ConfigurationDate
where Year > 2012 OR ( Year = 2012 AND Month >= 9 )
Order By Year,Month ASC
于 2013-10-13T03:08:38.570 回答