1

在标记为重复问题之前,请仔细阅读。我在这里有不同的问题。我想使用 yii2 搜索模型生成这种查询。

select * from t1 innerjoin t2 on (t1.id = t2.id) where ((t1.price >= '1000' and t1.price <= '5000') OR 
                   ( t2.price >= '1000' and t2.price <= '5000' ))

在这里加入不是问题。主要问题是 where 子句。我试过这个但没有工作。

$query->andFilterWhere([
                     'and',
                     ['>=', 't1.price', $this>start_price],
                     ['<=', 't1.price', $this->end_price]
                 ])
      ->orFilterWhere([
                      'and',
                       ['>=', 't2.price', $this->start_price],
                       ['<=', 't2.price', $this->end_price]
              ]);
4

1 回答 1

16

尝试

$query->andFilterWhere([
    'or',
    [
        'and',
        ['>=', 't1.price', $this>start_price],
        ['<=', 't1.price', $this->end_price]
    ],
    [
        'and',
        ['>=', 't2.price', $this->start_price],
        ['<=', 't2.price', $this->end_price]
    ]
]);
于 2016-07-26T10:21:47.110 回答