Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command::update() - add ability to pass Query to UPDATE FROM #94

Open
exru opened this issue Dec 1, 2015 · 2 comments
Open

Command::update() - add ability to pass Query to UPDATE FROM #94

exru opened this issue Dec 1, 2015 · 2 comments

Comments

@exru
Copy link

exru commented Dec 1, 2015

$db = db()->createCommand("
    UPDATE     bet 
    SET        status = subquery.winr
    FROM       (:subquery) AS subquery 
    WHERE      bet.id = subquery.id
", ['subquery' => new Expression($this->leadersQuery()->select(['id', 'winr'])->createCommand()->rawSql)])->execute();

//->update(/*the truth is out there*/); needed
@SilverFire SilverFire changed the title createCommand update from feature Command::update() - add ability to pass Query instead of table name Dec 1, 2015
@SilverFire
Copy link
Member

As a result of #10287 and Gitter talk.

@exru needs to make an UPDATE query from another query.

Assume we have the following query:

$query = new \yii\db\Query()
          ->select(['id', 'winr'])
          ->from('leaders')
          ->where(['state' => User::STATE_BLOCKED]);
// yeah, it's a simple one and we manage with it without the feature,
// but imagine that it's huge, with tons of joins, expressions in select, etc

and we need to update another table from query. The expected SQL should be like @exru posted in the header.

@exru suggests to add a chained update() to Query:

     $query->update('bet', ['status' => new Expression('subquery.winr')], 'id = subquery.id');

But I prefer to modify Command::update() to pass query in addition to the table name

     $queryBuilder->update(['bet', 'subquery' => $query], ['status' => new Expression('subquery.winr')], 'id = subquery.id');

or

     $queryBuilder->update('bet', ['status' => new Expression('subquery.winr')], 'id = subquery.id')->from(['subquery' => $query]);

@yiisoft/core-developers

@SilverFire SilverFire changed the title Command::update() - add ability to pass Query instead of table name Command::update() - add ability to pass Query to UPDATE FROM Dec 1, 2015
@exru
Copy link
Author

exru commented Dec 1, 2015

I guess be appropriate the
$db->update('table_name',['set'], ['where'], ['from'=>$query]);
or
$db->update(['table_name', 'from'=>$query],['set'], ['where'], ['params']);

@samdark samdark transferred this issue from yiisoft/yii2 Apr 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants