php - 如何在 yii2 的 AndWhere 中使用嵌套条件

标签 php yii2 yii2-model

在标记为重复问题之前,请阅读它。我在这里有不同的问题。我想使用 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]
              ]);

最佳答案

尝试

$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]
    ]
]);

关于php - 如何在 yii2 的 AndWhere 中使用嵌套条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38586372/

相关文章:

php - 如何通过composer安装eZ Platform

php - 试图让 php 查询每天只发生一次

php - 具有自定义属性的 Magento 产品网格过滤器

php - 在 mysql 中进行平局排名并跳过不满足某个条件的某些行

php - 如何从模型打印 Yii2 GridView 中的第一个提醒、第二个提醒?

Yii2 唯一验证器被忽略

javascript - yii2 PJAX 如何在 pjax 重新加载后显示模态?

mysql - 如何使用 yii2 框架在数据库中的邮件正文中显示图像

javascript - Yii2 通过点击链接加载模态

mysql - 如何删除yii2 Controller 中两个表中的数据