dousi8237 2016-07-26 09:42
浏览 754
已采纳

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

Before marking as duplicate question,kindly read it. I have different problem here. I want to generate this kind of Query using yii2 search model.

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' ))

Joining is not a problem here. The main problem is the where clause. I tried this but not working.

$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]
              ]);
  • 写回答

1条回答 默认 最新

  • duanpiao6679 2016-07-26 10:21
    关注

    Try

    $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]
        ]
    ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题