Yii2 QueryBuilder Update with Join

前端 未结 1 593
挽巷
挽巷 2021-01-13 02:09

I have the following raw SQL query:

UPDATE `pay_audit`
JOIN `invoice_items`
ON `invoice_items`.`mdn` = `pay_audit`.`account_id` 
AND `invoice_items`.`unitpri         


        
相关标签:
1条回答
  • 2021-01-13 02:41

    I'm afraid Yii 2 Query Builder is for select queries only.

    For update queries you have at least three options:

    • Raw SQL:

      \Yii::$app->db->createCommand('update user set status = 1 where age > 30')->execute();
      
    • Raw SQL with placeholders (to prevent SQL injection)

      \Yii::$app->db->createCommand('update user set status = :status where age > 30')->bindValue(':status','1')->execute();
      
    • update() method

      // update user set status = 1 where age > 30
      \Yii::$app->db->createCommand()->update('user', ['status' => 1], 'age > 30')->execute();
      

    More info here:

    • Yii DAO

    • Another question on SO

    0 讨论(0)
提交回复
热议问题