$query = $table::find()
->select('id')
->where([
'and',
[
'or',
['time_type' => 1],
[
'and',
[
'time_type' => 2,
],
[
'or',
[
'and',
['<=', 'start_time', $this->start_time],
['>=', 'end_time', $this->start_time],
],
[
'and',
['<=', 'start_time', $this->end_time],
['>=', 'end_time', $this->end_time],
],
],
],
[
'and',
// 主要是这里 不需要转义的字段/值可以加一个()小括号就可以了
// 比较类型 字段 值可以写成具体的变量
// ['=', '(1)', 1],
['(1)' => 1],
['<=', 'start_time', $time],
['>=', 'end_time', $time],
],
],
])
// 值不为空就添加上条件
->andFilterWhere(['<>', 'id', $this->id]);
->exists();
不需要转义的字段/值可以加一个()小括号就可以了 类似这样
?
生成的sql大概是这个样子
SELECT
`id`
FROM
`table_name`
WHERE
((
`time_type` = 1
)
OR ((
`time_type` = 2
)
AND (((
`start_time` <= 1649407080 ) AND ( `end_time` >= 1649407080
))
OR ((
`start_time` <= 1650616680 ) AND ( `end_time` >= 1650616680
))))
OR (((
1
)= 1
)
AND ( `start_time` <= 1646993148 )
AND ( `end_time` >= 1646993148 )))
|