代码之家  ›  专栏  ›  技术社区  ›  moh

yii2 right sql命令来执行我的查询

  •  0
  • moh  · 技术社区  · 7 年前

    我正在尝试从 user_request_wallet 表中lead_developer=?然后回答什么?或者是?我想加入 mainRequest

    它应该怎么指挥???

    我试了两种方法,但没有成功

    //$model = UserRequestWallet::find()->Where(['and',['lead_developer' => $getUser]])
    //->andWhere(['and',['answer_tocall' => 'accepted']])
    //->andWhere(['or',['answer_tocall' => 'putonhold']])
    //->joinWith('mainRequest');
    
    $model = (new \yii\db\Query())
    ->select('*')
    ->from('user_request_wallet')
    ->andwhere(['lead_developer'=> $getUser])->andWhere(['answer_tocall' => 'accepted'])
    ->orWhere(['answer_tocall'=>'putonhold'])->leftJoin('mainRequest');
    

    Exception (Database Exception) 'yii\db\Exception' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE ((`lead_developer`=7) AND (`answer_tocall`='accepted')) OR (`answer_tocall' at line 1
    

    正在执行的SQL是:

    SELECT COUNT(*) FROM `user_request_wallet` LEFT JOIN `mainRequest` WHERE ((`lead_developer`=7) AND (`answer_tocall`='accepted')) OR (`answer_tocall`='putonhold')
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   Insane Skull    7 年前
    $model = UserRequestWallet::find()
            ->with('mainRequest')
            ->where(['lead_developer' => $getUser])
            ->andWhere([
                'OR',
                ['answer_tocall' => 'accepted'],
                ['answer_tocall' => 'putonhold'],
            ])
            ->all();
    
        2
  •  0
  •   Paul    7 年前

    这就是工作

    $model = UserRequestWallet::find()
        ->where(['lead_developer' => $getUser])
        ->andWhere(['or', 'answer_tocall = accepted', 'answer_tocall = putonhold'])
        ->joinWith('mainRequest');
    
        3
  •  0
  •   vityapro    7 年前

    根据 documentation leftJoin( $table, $on = '', $params = [] ) 如果你想使用 static method find() joinWith(['relationName']) 属于 ActiveRecords documentation .