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

报告相同问题?

悬赏问题

  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误