updateFromSelect

Updated . Posted . Visible to the public.

How to update a column in main table from another table's column. Reference:
Varien_Db_Adapter_Pdo_Mysql::updateFromSeelct()

    /**
     * UPDATE maintable t1
     * JOIN sales_flat_order t2 ON t1.application_id = t2.increment_id
     * SET t1.stud_id = t2.appl_id
     * WHERE t1.student_id IS NULL               
     */         
    public function updateStudId()
    {
        $adapter = $this->_getWriteAdapter();
        $select = $adapter->select()
            ->from($this->getMainTable(), array())
            ->joinLeft(array('t2'=>$this->getTable('sales/order')),
                        "t1.application_id = t2.increment_id",
                        array('stud_id' => 'appl_id'))
            ->where('t1.stud_id IS NULL');

        $query = $adapter->updateFromSelect(
            $select,
            array('t1' => $this->getMainTable())
        );

        $adapter->query($query);
        return $this;           
    }

The query string $query is

UPDATE `maintable` AS `t1`
INNER JOIN `maintable`
LEFT JOIN `sales_flat_order` AS `t2` ON t1.application_id = t2.increment_id
SET `t1`.`student_id` = `t2`.`applicant_id`
WHERE (t1.student_id IS NULL)

How to update a column to a constant

$installer = Mage::getResourceModel('core/setup', 'emgs_setup');
$db = $installer->getConnection();
$mainTable = $installer->getTable('catalog/product_option');
$select = $db->select()
	->from($mainTable, array())
	->joinLeft(array('t2'=>$installer->getTable('catalog/product_option_title')),
				"t1.option_id = t2.option_id",
				array('type' => new Zend_Db_Expr('"country"')))
	->where('t2.title LIKE "%Country%"');

$query = $db->updateFromSelect(
	$select,
	array('t1' => $mainTable)
);

The query string $query is

UPDATE `catalog_product_option` AS `t1`
 INNER JOIN `catalog_product_option`
 LEFT JOIN `catalog_product_option_title` AS `t2` ON t1.option_id = t2.option_id
SET `t1`.`type` = "country"
WHERE (t2.title LIKE "%Country%")
kiatng
Last edit
kiatng
Posted by kiatng to OpenMage (2015-11-20 08:17)