Is there a way to perform a two-table SQL Update statement using functions in Database.php?
update table1, table2 set table1.x = table2.y where table1.foo = table2.bar
Function Database.php::update() appears to accept only a single table name. (This is in MW 1.12.)
Thanks. DanB
Looks like there's no built-in way to do a multi-table update, but you could construct such a function in a manner such as this:
function updateMultiple( &$dbw, $tables, $values, $conds, $fname = null, $options = array() ) { if (!$fname) $fname = __METHOD__; $tables = $dbw->tableNamesWithUseIndexOrJOIN( $tables ); $opts = $dbw->makeUpdateOptions( $options ); $sql = "UPDATE $opts $tables SET " . $dbw->makeList( $values, LIST_SET ); if ( $conds != '*' ) { $sql .= " WHERE " . $dbw->makeList( $conds, LIST_AND ); } return $dbw->query( $sql, $fname ); }
Then
$dbw =& wfGetDB( DB_MASTER ); updateMultiple( $dbw, array( 'table1', 'table2' ), "table1.x = table2.y", "table1.foo = table2.bar", __METHOD__ );
Disclaimer: I have not tested the above at all - mostly just copy/pasted from Database::update.
-- Jim R. Wilson (jimbojw)
On Wed, Jul 16, 2008 at 8:36 AM, Daniel Barrett danb@vistaprint.com wrote:
Is there a way to perform a two-table SQL Update statement using functions in Database.php?
update table1, table2 set table1.x = table2.y where table1.foo = table2.bar
Function Database.php::update() appears to accept only a single table name. (This is in MW 1.12.)
Thanks. DanB
MediaWiki-l mailing list MediaWiki-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-l
Thanks Jim, that's pretty much what I did (while awaiting a better method). I appreciate the help.
DanB
-----Original Message----- function updateMultiple( &$dbw, $tables, $values, $conds, $fname = null, $options = array() ) { if (!$fname) $fname = __METHOD__; $tables = $dbw->tableNamesWithUseIndexOrJOIN( $tables ); $opts = $dbw->makeUpdateOptions( $options ); $sql = "UPDATE $opts $tables SET " . $dbw->makeList( $values, LIST_SET ); if ( $conds != '*' ) { $sql .= " WHERE " . $dbw->makeList( $conds, LIST_AND ); } return $dbw->query( $sql, $fname ); }
mediawiki-l@lists.wikimedia.org