我想选择两个不同日期(datetime
类型在数据库中)的项目。在这种情况下,您希望选择dateStart
和dateEnd
范围内的所有项目。
id | dateStart | dateEnd
---+------------+-----------+
3 | 2017-01-05 | 2017-01-07
6 | 2017-01-06 | 2017-01-10
8 | 2017-01-05 | 2017-01-11
11 | 2017-01-03 | 2017-01-04
12 | 2017-01-04 | 2017-01-10
15 | 2017-01-04 | 2017-01-18
17 | 2017-01-08 | 2017-01-10
25 | 2017-01-12 | 2017-01-15
31 | 2017-01-11 | 2017-01-24
请注意,我不希望在以下日期之间进行搜索:
Model::find()
->where(['between', 'dateStart', "2017-01-06", "2017-02-11"])
->all();
..因为between
不是in range
。
id | dateStart | dateEnd
---+------------+-----------+
6 | 2017-01-06 | 2017-01-10
8 | 2017-01-05 | 2017-01-11
11 | 2017-01-03 | 2017-01-04
12 | 2017-01-04 | 2017-01-10
15 | 2017-01-04 | 2017-01-18
通过搜索startDate> = 2017-01-05
和endDate< = 2017-01-11
选择的项目(基于上面的表数据)应该如下所示:
id | dateStart | dateEnd
---+------------+-----------+
3 | 2017-01-05 | 2017-01-07
6 | 2017-01-06 | 2017-01-10
8 | 2017-01-05 | 2017-01-11
12 | 2017-01-04 | 2017-01-10
15 | 2017-01-04 | 2017-01-18
17 | 2017-01-08 | 2017-01-10
31 | 2017-01-11 | 2017-01-24
答案 0 :(得分:2)
使用ActiveRecord,您可以执行以下子句序列 - >或者:
$dateStart = '2017-01-05';
$dateEnd = '2017-01-11';
Model::find()
->orWhere(['between', 'dateEnd', $dateStart , $dateEnd])
->orWhere(['between', 'dateStart', $dateStart , $dateEnd])
->orWhere(['and', "dateEnd>='".$dateEnd."'", "dateStart<='".$dateStart ."'"])
->all();