0

这是我的桌子:

ut_period

id | month | year | name 
1  | 9     | 2013 | dddd
2  | 2     | 2017 | eeee

我想在 2013-8 和 2018-9 选择行女巫。所以我写了这个查询,但没有成功:

SELECT * 
FROM `ut_period` p  
WHERE `p`.`status` = 4 
  AND `p`.`year` >= '2013' 
  AND `p`.`mont` >= '8'
  AND `p`.`year` <= '2018' 
  AND `p`.`month` <= '9' 

我该怎么做?

4

2 回答 2

1

I find the easiest way to handle "year-months" is by multiplications -- essentially, number of months since some date:

select p.*
from ut_period p
where (p.year * 12 + p.month) between 2013*12 + 8  and 2018*12 + 9;

If you like, you can also convert these to numbers of the form YYYYMM, which makes it easier to put in the constant values for the range:

select p.*
from ut_period p
where (p.year * 100 + p.month) between 201308  and 201809;
于 2013-08-31T14:09:16.750 回答
0

使用BETWEEN月份和年份的子句

文档在这里

此外,您真的应该将日、月和年放在一个列中——这就是 MySQL 具有与日期相关的字段类型的原因。将日期、月份和年份替换为具有 DATE 字段类型的单个列,然后您可以更轻松地比较日期。

于 2013-08-31T14:07:34.937 回答